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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc))]
#![deny(missing_docs)]
#![doc(html_root_url = "https://docs.rs/throw/0.1.7")]
//! Throw!
//! ------
//!
//! Throw is a new experimental rust error handling library, meant to assist and build on existing
//! error handling systems.
//!
//! Throw exports two structs, `throw::ErrorPoint` and `throw::Error`. `throw::Error` stores a
//! single `original_error` variable which it is created from, and then a list of `ErrorPoint`s
//! which starts out with the original point of creation with `throw!()`, and is added to every
//! time you propagate the error upwards with `up!()`.
//!
//! *Throw does not replace existing error handling systems*. The `throw::Error` type has a type
//! parameter `E` which represents an internal error type stored. `throw::Error` just wraps your
//! error type and stores ErrorPoints alongside it.
//!
//! Throw helps you better keep track of your errors. Instead of seeing a generic "No such file or
//! directory" message, you get a stack trace of functions which propagated the error as well.
//!
//! Instead of:
//!
//! ```text
//! IO Error: failed to lookup address information: Name or service not known
//! ```
//!
//! Get:
//!
//! ```text
//! Error: IO Error: failed to lookup address information: Name or service not known
//!     at 79:17 in zaldinar::startup (src/startup.rs)
//!     at 104:4 in zaldinar::startup (src/startup.rs)
//!     at 28:17 in zaldinar_irclib (/home/daboross/Projects/Rust/zaldinar/zaldinar-irclib/src/lib.rs)
//! ```
//!
//! ---
//!
//! Using throw!
//! ---
//!
//! The main way you use throw is through two macros, `throw!()` and `up!()`. `throw!()` is used
//! when you have a regular (non-throw) result coming from some library function that you want to
//! propagate upwards in case of an error. `up!()` is used when you have an error which was
//! created using `throw!()` in a sub-function which you want to add an error point to and
//! propagate upwards.
//!
//! Here's an example of throw in action:
//!
//! ```rust
//! #[macro_use]
//! extern crate throw;
//!
//! use std::io::prelude::*;
//! use std::io;
//! use std::fs::File;
//!
//! fn read_log() -> Result<String, throw::Error<io::Error>> {
//!     let mut file = throw!(File::open("some_file.log"));
//!     let mut buf = String::new();
//!     throw!(file.read_to_string(&mut buf));
//!     Ok((buf))
//! }
//!
//! fn do_things() -> Result<(), throw::Error<io::Error>> {
//!     let log_contents = up!(read_log());
//!     println!("Log contents: {}", log_contents);
//!
//!     Ok(())
//! }
//!
//! fn main() {
//!     let result = do_things();
//!     if let Err(e) = result {
//! #       /*
//!         panic!("{}", e);
//! #       */
//! #       let err = e.to_string();
//! #       assert!(err.starts_with("Error: No such file or directory (os error 2)\
//! #       \n\tat "), "mangled error message: {}", err);
//!     }
//! }
//! ```
//!
//! This simple program behaves exactly as if `Result<_, io::Error>` directly when it functions
//! correctly. When the program encounters is when throw really shines.  This will result in an
//! error message:
//!
//! ```text
//! Error: No such file or directory (os error 2)
//!    at 16:23 in main (src/main.rs)
//!    at 9:19 in main (src/main.rs)
//! ```
//!
//! These stack traces are stored inside throw::Error, and are recorded automatically when
//! `throw!()` or `up!()` returns an Err value.
//!
//! In each `at` line, the `16:23` represents `line_num:column_num`, the `main` represents the
//! module path (for example `my_program::sub_module`), and `src/main.rs` represents the path of
//! the file in which `throw!()` was used in.
//!
//! ---
//!
//! Throwing directly from a function is also supported, using `throw_new!()`:
//!
//! ```
//! # #[macro_use]
//! # extern crate throw;
//! fn possibly_fails() -> Result<(), throw::Error<&'static str>> {
//!     if true {
//!         // throw_new!() will always return directly
//!         throw_new!("oops");
//!     }
//!
//!     Ok(())
//! }
//!
//! fn main() {
//! #   /*
//!     possibly_fails().unwrap()
//! #   */
//! #   let err = possibly_fails().unwrap_err().to_string();
//! #   assert!(err.starts_with("Error: oops\n\tat "), "mangled error message: {}",  err);
//! }
//! ```
//!
//! ```text
//! called `Result::unwrap()` on an `Err` value: Error: "oops"
//!    at 6:8 in main (src/main.rs)
//! ```
//!
//! `throw_new!()` differs from `throw!()` in that it takes a parameter directly to pass to a
//! `throw::Error`, rather than a `Result<>` to match on. `throw_new!()` will always return
//! directly from the function.
//!
//! ---
//!
//! `no_std`
//! ---
//!
//! Throw offers support for `no_std`, with the caveat that a dependency on `alloc` is still
//! required for `Vec` support. (`throw` uses a Vec to store error points within an error.)
//!
//! To use this feature, depend on `throw` with `default-features = false`:
//!
//! ```toml
//! [dependencies]
//! throw = { version = "0.1", default-features = "false" }
//! ```
//!
//! ---
//!
//! Key/value pairs
//! ---
//!
//! Throw supports adding key/value pairs to errors to provide additional context information.
//! In order to use this, simply add any number of `"key_name" => value,` arguments to any of
//! the macros throw exports. `value` can be any integer type, float type, an `&'static str`,
//! or an owned string.
//!
//! ```
//! # #[macro_use]
//! # extern crate throw;
//! fn possibly_fails(process_this: &str) -> Result<(), throw::Error<&'static str>> {
//!     if true {
//!         throw_new!("oops", "processing" => process_this.to_owned());
//!     }
//!
//!     Ok(())
//! }
//!
//! fn main() {
//! #   /*
//!     possibly_fails("hello").unwrap()
//! #   */
//! #   let err = possibly_fails("hello").unwrap_err().to_string();
//! #   assert!(err.contains("processing: hello"), "mangled error message: {}",  err);
//! }
//! ```
//!
//! Results in:
//!
//! ```text
//! thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error: "oops"
//!     processing: hello
//!     at 6:9 in rust_out (src/lib.rs)', libcore/result.rs:945:5
//! ```
//!
//! ---
//!
//! Serde support
//! ---
//!
//! To have `serde::{Serialize, Deserialize}` implemented on Throw types, depend on throw with
//! `features = ["serde-1-std"]` or `features = ["serde-1"]` for no-std environments.

#[cfg(not(feature = "std"))]
#[cfg_attr(any(feature = "serde-1", feature = "serde-1-std"), macro_use)]
extern crate alloc;

#[cfg(any(feature = "serde-1", feature = "serde-1-std"))]
extern crate serde;
#[cfg(any(feature = "serde-1", feature = "serde-1-std"))]
#[macro_use]
extern crate serde_derive;

#[cfg(feature = "std")]
mod core {
    pub use std::fmt;
    pub use std::result;
}

use core::fmt;

#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

#[cfg(any(feature = "serde-1", feature = "serde-1-std"))]
use serde::ser::{Serialize, SerializeStruct, Serializer};

/// Types allowed to be value in the context vector
#[derive(Debug, Clone)]
#[cfg_attr(
    any(feature = "serde-1", feature = "serde-1-std"),
    derive(Serialize)
)]
#[cfg_attr(
    any(feature = "serde-1", feature = "serde-1-std"),
    serde(untagged)
)]
pub enum ThrowContextValues {
    /// Boolean context value
    Bool(bool),
    /// 8-bit signed context value
    Int8(i8),
    /// 8-bit unsigned context value
    Uint8(u8),
    /// 16-bit signed context value
    Int16(i16),
    /// 16-bit unsigned context value
    Uint16(u16),
    /// 32-bit signed context value
    Int32(i32),
    /// 32-bit unsigned context value
    Uint32(u32),
    /// 64-bit signed context value
    Int64(i64),
    /// 64-bit unsigned context value
    Uint64(u64),
    /// 32-bit floating point context value
    Float32(f32),
    /// 64-bit floating point context value
    Float64(f64),
    /// Allocated string context value
    String(String),
    /// Static / program inline string context value
    StaticStr(&'static str),
}

impl fmt::Display for ThrowContextValues {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ThrowContextValues::Bool(ref x) => write!(f, "{}", x),
            ThrowContextValues::Int8(ref x) => write!(f, "{}", x),
            ThrowContextValues::Uint8(ref x) => write!(f, "{}", x),
            ThrowContextValues::Int16(ref x) => write!(f, "{}", x),
            ThrowContextValues::Uint16(ref x) => write!(f, "{}", x),
            ThrowContextValues::Int32(ref x) => write!(f, "{}", x),
            ThrowContextValues::Uint32(ref x) => write!(f, "{}", x),
            ThrowContextValues::Int64(ref x) => write!(f, "{}", x),
            ThrowContextValues::Uint64(ref x) => write!(f, "{}", x),
            ThrowContextValues::Float32(ref x) => write!(f, "{}", x),
            ThrowContextValues::Float64(ref x) => write!(f, "{}", x),
            ThrowContextValues::String(ref x) => write!(f, "{}", x),
            ThrowContextValues::StaticStr(ref x) => write!(f, "{}", x),
        }
    }
}

impl Into<ThrowContextValues> for u8 {
    fn into(self) -> ThrowContextValues {
        ThrowContextValues::Uint8(self)
    }
}

impl Into<ThrowContextValues> for i8 {
    fn into(self) -> ThrowContextValues {
        ThrowContextValues::Int8(self)
    }
}

impl Into<ThrowContextValues> for u16 {
    fn into(self) -> ThrowContextValues {
        ThrowContextValues::Uint16(self)
    }
}

impl Into<ThrowContextValues> for i16 {
    fn into(self) -> ThrowContextValues {
        ThrowContextValues::Int16(self)
    }
}

impl Into<ThrowContextValues> for u32 {
    fn into(self) -> ThrowContextValues {
        ThrowContextValues::Uint32(self)
    }
}

impl Into<ThrowContextValues> for i32 {
    fn into(self) -> ThrowContextValues {
        ThrowContextValues::Int32(self)
    }
}

impl Into<ThrowContextValues> for u64 {
    fn into(self) -> ThrowContextValues {
        ThrowContextValues::Uint64(self)
    }
}

impl Into<ThrowContextValues> for i64 {
    fn into(self) -> ThrowContextValues {
        ThrowContextValues::Int64(self)
    }
}

impl Into<ThrowContextValues> for f32 {
    fn into(self) -> ThrowContextValues {
        ThrowContextValues::Float32(self)
    }
}

impl Into<ThrowContextValues> for f64 {
    fn into(self) -> ThrowContextValues {
        ThrowContextValues::Float64(self)
    }
}

impl<'a> Into<ThrowContextValues> for &'static str {
    fn into(self) -> ThrowContextValues {
        ThrowContextValues::StaticStr(self)
    }
}

impl Into<ThrowContextValues> for String {
    fn into(self) -> ThrowContextValues {
        ThrowContextValues::String(self)
    }
}

/// Result alias for a result containing a throw::Error.
pub type Result<T, E> = core::result::Result<T, Error<E>>;

/// Represents a location at which an error was thrown via throw!()
#[derive(Debug)]
#[cfg_attr(
    any(feature = "serde-1", feature = "serde-1-std"),
    derive(Serialize)
)]
pub struct ErrorPoint {
    line: u32,
    column: u32,
    module_path: &'static str,
    file: &'static str,
}

impl ErrorPoint {
    /// The line throw!() occurred at, retrieved by line!()
    #[inline]
    pub fn line(&self) -> u32 {
        self.line
    }

    /// The column throw!() occurred at, retrieved by column!()
    #[inline]
    pub fn column(&self) -> u32 {
        self.column
    }

    /// The module throw!() occurred in, retrieved by module_path!()
    #[inline]
    pub fn module_path(&self) -> &'static str {
        self.module_path
    }

    /// The file throw!() occurred in, retrieved by file!()
    #[inline]
    pub fn file(&self) -> &'static str {
        self.file
    }

    #[doc(hidden)]
    pub fn __construct(
        line: u32,
        column: u32,
        module_path: &'static str,
        file: &'static str,
    ) -> ErrorPoint {
        ErrorPoint {
            line: line,
            column: column,
            module_path: module_path,
            file: file,
        }
    }
}

/// represent a key-value pair
#[derive(Debug, Clone)]
#[cfg_attr(
    any(feature = "serde-1", feature = "serde-1-std"),
    derive(Serialize)
)]
pub struct KvPair {
    key: &'static str,
    value: ThrowContextValues,
}

impl KvPair {
    /// Creates a new key value pair
    fn new(key: &'static str, value: ThrowContextValues) -> KvPair {
        KvPair { key, value }
    }

    /// Retrieve the key associated with this `KvPair`.
    pub fn key(&self) -> &'static str {
        self.key
    }

    /// Retrieve the value associated with this `KvPair`.
    pub fn value(&self) -> &ThrowContextValues {
        &self.value
    }
}

/// Represents an error. Stores an original error of type E, and any number of ErrorPoints at
/// which the error was propagated.

pub struct Error<E> {
    points: Vec<ErrorPoint>,
    context: Vec<KvPair>,
    error: E,
}

#[cfg(any(feature = "serde-1", feature = "serde-1-std"))]
impl<E: fmt::Display> Serialize for Error<E> {
    fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("Error", 3)?;

        state.serialize_field("points", &self.points)?;
        state.serialize_field("context", &self.context)?;
        state.serialize_field::<&str>("error", &format!("{}", self.error).as_str())?;
        state.end()
    }
}

impl<E> Error<E> {
    /// Creates a new Error with no ErrorPoints
    pub fn new(error: E) -> Error<E> {
        Error {
            points: Vec::new(),
            context: Vec::new(),
            error: error,
        }
    }

    /// get context
    pub fn get_context(&self) -> &[KvPair] {
        self.context.as_slice()
    }

    /// For macro use only
    #[doc(hidden)]
    pub fn add_context<V: Into<ThrowContextValues>>(&mut self, key: &'static str, value: V) {
        self.context.push(KvPair::new(key, value.into()))
    }

    /// For macro use only
    #[doc(hidden)]
    pub fn __push_point(&mut self, point: ErrorPoint) {
        self.points.push(point);
    }

    /// Gets all ErrorPoints where this Error was thrown. These are in reverse order, with the
    /// first time it was thrown first and the latest time it was thrown last.
    #[inline]
    pub fn points(&self) -> &[ErrorPoint] {
        &self.points
    }

    /// Gets the original error which this Error was constructed with.
    #[deprecated = "use `error` instead."]
    #[inline]
    pub fn original_error(&self) -> &E {
        self.error()
    }

    /// Gets the original error which this Error was constructed with.
    #[inline]
    pub fn error(&self) -> &E {
        &self.error
    }

    /// Move the original error out.
    #[inline]
    pub fn into_origin(self) -> E {
        self.into_error()
    }

    /// Take out the original error and transform into another type
    /// where the original error can transform into that type.
    #[inline]
    pub fn into_error<N>(self) -> N
    where
        E: Into<N>,
    {
        self.error.into()
    }

    /// Transforms this Error<OldError> into Error<NewError>. This isn't implemented as an Into or
    /// From implementation because it would conflict with the blanket implementations in stdlib.
    pub fn transform<NE>(self) -> Error<NE>
    where
        E: Into<NE>,
    {
        Error {
            points: self.points,
            context: self.context,
            error: self.error.into(),
        }
    }
}

impl<E> fmt::Display for Error<E>
where
    E: fmt::Display,
{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(fmt, "Error: {}", self.error));

        for kv in self.context.iter().rev() {
            try!(write!(fmt, "\n\t{}: {}", kv.key(), kv.value(),));
        }

        for point in self.points.iter().rev() {
            try!(write!(
                fmt,
                "\n\tat {}:{} in {} ({})",
                point.line(),
                point.column(),
                point.module_path(),
                point.file()
            ));
        }

        Ok(())
    }
}

impl<E> fmt::Debug for Error<E>
where
    E: fmt::Debug,
{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(fmt, "Error: {:?}", self.error));
        for kv in self.context.iter().rev() {
            try!(write!(fmt, "\n\t{}: {}", kv.key(), kv.value(),));
        }
        for point in self.points.iter().rev() {
            try!(write!(
                fmt,
                "\n\tat {}:{} in {} ({})",
                point.line(),
                point.column(),
                point.module_path(),
                point.file()
            ));
        }

        Ok(())
    }
}

#[cfg(feature = "std")]
impl<E> std::error::Error for Error<E>
where
    E: std::error::Error
{
    fn description(&self) -> &str {
        self.error().description()
    }

    fn cause(&self) -> Option<&std::error::Error> {
        Some(self.error())
    }
}

#[macro_export]
macro_rules! up {
    ($e:expr) => (
        match $e {
            Ok(v) => v,
            Err(e) => {
                // re-assignment for a better error message if up!() is used incorrectly
                return Err(__with_new_errorpoint!(e.transform()));
            },
        }
    );
    ($e:expr, $($key:expr => $value:expr),+) => (
        match $e {
            Ok(v) => v,
            Err(e) => {
                // re-assignment for a better error message if up!() is used incorrectly
                let mut me = __with_new_errorpoint!(e.transform());
                $(
                    me.add_context($key, $value);
                )*
                return Err(me);
            },
        }
    );
}

#[doc(hidden)]
#[macro_export]
macro_rules! __with_new_errorpoint {
    ($e:expr) => {{
        let mut e = $e;
        e.__push_point($crate::ErrorPoint::__construct(
            line!(),
            column!(),
            module_path!(),
            file!(),
        ));
        e
    }};
}

#[macro_export]
macro_rules! throw {
    ($e:expr) => (
        match $e {
            Ok(v) => v,
            Err(e) => throw_new!(e),
        }
    );
    ($e:expr, $($key:expr => $value:expr),+ $(,)*) => ({
         match $e {
            Ok(v) => v,
            Err(e) => throw_new!(e, $($key => $value,)*),
        }
    });
}

#[macro_export]
macro_rules! throw_new {
    ($e:expr) => ({
        return Err(__with_new_errorpoint!($crate::Error::new($e.into())));
    });
    ($e:expr, $($key:expr => $value:expr),+ $(,)*) => ({
        let mut me = $crate::Error::new($e.into());
        $(
            me.add_context($key, $value);
        )*
        return Err(__with_new_errorpoint!(me));
    });
}