summaryrefslogtreecommitdiffstats
path: root/src/fail.rs
blob: 519de19710dbdcbd323f929496b66158c8e509f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use failure;
use failure::Fail;
//use failure::Backtrace;
//use async_value::AError;


use termion::event::Key;

use std::path::PathBuf;
use std::sync::{Arc, Mutex};

use crate::foldview::LogEntry;
use crate::mediaview::MediaError;

pub type HResult<T> = Result<T, HError>;



#[derive(Fail, Debug, Clone)]
pub enum HError {
    #[fail(display = "IO error: {} ", _0)]
    IoError(String),
    #[fail(display = "Mutex failed")]
    MutexError,
    #[fail(display = "Can't lock!")]
    TryLockError,
    #[fail(display = "Channel failed: {}", error)]
    ChannelTryRecvError{#[cause] error: std::sync::mpsc::TryRecvError},
    #[fail(display = "Channel failed: {}", error)]
    ChannelRecvError{#[cause] error: std::sync::mpsc::RecvError},
    #[fail(display = "Channel failed")]
    ChannelSendError,
    #[fail(display = "Timer ran out while waiting for message on channel!")]
    ChannelRecvTimeout(#[cause] std::sync::mpsc::RecvTimeoutError),
    #[fail(display = "Previewer failed on file: {}", file)]
    PreviewFailed{file: String},
    #[fail(display = "StalePreviewer for file: {}", file)]
    StalePreviewError{file: String},
    #[fail(display = "Accessed stale value")]
    StaleError,
    #[fail(display = "Failed: {}", _0)]
    Error(String),
    #[fail(display = "Was None!")]
    NoneError,
    #[fail(display = "Async Error: {}", _0)]
    AError(async_value::AError),
    #[fail(display = "No widget found")]
    NoWidgetError,
    #[fail(display = "Path: {:?} not in this directory: {:?}", path, dir)]
    WrongDirectoryError{ path: PathBuf, dir: PathBuf},
    #[fail(display = "Widget finnished")]
    PopupFinnished,
    #[fail(display = "No completions found")]
    NoCompletionsError,
    #[fail(display = "No more history")]
    NoHistoryError,
    #[fail(display = "No core for widget")]
    NoWidgetCoreError,
    #[fail(display = "No header for widget")]
    NoHeaderError,
    #[fail(display = "You wanted this!")]
    Quit,
    #[fail(display = "HBox ratio mismatch: {} widgets, ratio is {:?}", wnum, ratio)]
    HBoxWrongRatioError{ wnum: usize, ratio: Vec<usize> },
    #[fail(display = "Got wrong widget: {}! Wanted: {}", got, wanted)]
    WrongWidgetError{got: String, wanted: String},
    #[fail(display = "Strip Prefix Error: {}", error)]
    StripPrefixError{#[cause] error: std::path::StripPrefixError},
    #[fail(display = "INofify failed: {}", _0)]
    INotifyError(String),
    #[fail(display = "Tags not loaded yet")]
    TagsNotLoadedYetError,
    #[fail(display = "Undefined key: {:?}", key)]
    WidgetUndefinedKeyError{key: Key},
    #[fail(display = "Terminal has been resized!")]
    TerminalResizedError,
    #[fail(display = "Widget has been resized!")]
    WidgetResizedError,
    #[fail(display = "{}", _0)]
    Log(String),
    #[fail(display = "Metadata already processed")]
    MetadataProcessedError,
    #[fail(display = "No files to take from widget")]
    WidgetNoFilesError,
    #[fail(display = "Invalid line in settings file: {}", _0)]
    ConfigLineError(String),
    #[fail(display = "New input in Minibuffer")]
    MiniBufferInputUpdated(String),
    #[fail(display = "Failed to parse into UTF8")]
    UTF8ParseError(std::str::Utf8Error),
    #[fail(display = "Failed to parse integer!")]
    ParseIntError(std::num::ParseIntError),
    #[fail(display = "Failed to parse char!")]
    ParseCharError(std::char::ParseCharError),
    #[fail(display = "{}", _0)]
    Media(MediaError),
    #[fail(display = "{}", _0)]
    Mime(MimeError),
    #[fail(display = "{}", _0)]
    KeyBind(KeyBindError),
    #[fail(display = "FileBrowser needs to know about all tab's files to run exec!")]
    FileBrowserNeedTabFiles,
    #[fail(display = "{}", _0)]
    FileError(crate::files::FileError),
    #[fail(display = "{}", _0)]
    Nix(#[cause] nix::Error),
    #[fail(display = "Refresh parent widget!")]
    RefreshParent,
    #[fail(display = "Refresh parent widget!")]
    MiniBufferEvent(crate::minibuffer::MiniBufferEvent),
}

impl HError {
    pub fn log<T>(log: &str) -> HResult<T> {
        Err(HError::Log(String::from(log))).log_and()
    }
    pub fn quit() -> HResult<()> {
        Err(HError::Quit)
    }
    pub fn wrong_ratio<T>(wnum: usize, ratio: Vec<usize>) -> HResult<T> {
        Err(HError::HBoxWrongRatioError{ wnum: wnum, ratio: ratio })
    }
    pub fn no_widget<T>() -> HResult<T> {
        Err(HError::NoWidgetError)
    }
    pub fn wrong_widget<T>(got: &str, wanted: &str) -> HResult<T> {
        Err(HError::WrongWidgetError{ got: got.to_string(),
                                      wanted: wanted.to_string() })

    }
    pub fn popup_finnished<T>() -> HResult<T> {
        Err(HError::PopupFinnished)
    }
    pub fn tags_not_loaded<T>() -> HResult<T> {
        Err(HError::TagsNotLoadedYetError)
    }
    pub fn undefined_key<T>(key: Key) -> HResult<T> {
        Err(HError::WidgetUndefinedKeyError { key: key })
    }
    pub fn wrong_directory<T>(path: PathBuf, dir: PathBuf) -> HResult<T> {
        Err(HError::WrongDirectoryError{ path: path,
                                         dir: dir })

    }
    pub fn preview_failed<T>(file: &crate::files::File) -> HResult<T> {
        let name = file.name.clone();
        Err(HError::PreviewFailed{ file: name })

    }

    pub fn terminal_resized<T>() -> HResult<T> {
        Err(HError::TerminalResizedError)
    }

    pub fn widget_resized<T>() -> HResult<T> {
        Err(HError::WidgetResizedError)
    }

    pub fn stale<T>() -> HResult<T> {
        Err(HError::StaleError)
    }

    pub fn config_error<T>(line: String) -> HResult<T> {
        Err(HError::ConfigLineError(line))
    }

    pub fn metadata_processed<T>() -> HResult<T> {
        Err(HError::MetadataProcessedError)
    }

    pub fn no_files<T>() -> HResult<T> {
        Err(HError::WidgetNoFilesError)
    }

    pub fn input_updated<T>(input: String) -> HResult<T> {
        Err(HError::MiniBufferInputUpdated(input))
    }


}

#[derive(Fail, Debug, Clone)]
pub enum ErrorCause {
    #[fail(display = "{}", _0)]
    Str(String)
}


lazy_static! {
    static ref LOG: Mutex<Vec<LogEntry>> = Mutex::new(vec![]);
}

pub fn get_logs() -> HResult<Vec<LogEntry>> {
    let logs = LOG.lock()?.drain(..).collect();
    Ok(logs)
}

pub fn put_log<L: Into<LogEntry>>(log: L) -> HResult<()> {
    LOG.lock()?.push(log.into());
    Ok(())
}

pub trait ErrorLog where Self: Sized {
    fn log(self);
    fn log_and(self) -> Self;
}

// impl<T> ErrorLog for HResult<T> {
//     fn log(self) {
//         if let Err(err) = self {
//             put_log(&err).ok();
//         }
//     }

//     fn log_and(self) -> Self {
//         if let Err(err) = &self {
//             put_log(err).ok();
//         }
//         self
//     }
// }


// impl<T> ErrorLog for Result<T, AError> {
//     fn log(self) {
//         if let Err(err) = self {
//             put_log(&err.into()).ok();
//         }
//     }

//     fn log_and(self) -> Self {
//         if let Err(err) = &self {
//             put_log(&err.clone().into()).ok();
//         }
//         self
//     }
// }

impl<T, E> ErrorLog for Result<T, E>
where E: Into<HError> + Clone {
    fn log(self) {
        if let Err(err) = self {
            let err: HError = err.into();
            put_log(&err).ok();
        }
    }
    fn log_and(self) -> Self {
        if let Err(ref err) = self {
            let err: HError = err.clone().into();
            put_log(&err).ok();
        }
        self
    }
}

impl<E> ErrorLog for E
where E: Into<HError> + Clone {
    fn log(self) {
        let err: HError = self.into();
        put_log(&err).ok();

    }
    fn log_and(self) -> Self {
        let err: HError = self.clone().into();
        put_log(&err).ok();
        self
    }
}




impl From<std::io::Err