trimmer/
render_error.rs

1use std::fmt;
2use std::io;
3use std::error::Error;
4
5use Pos;
6
7
8quick_error! {
9    /// This error is used to describe invalid variable usage in template
10    #[derive(Debug)]
11    pub enum DataError {
12        /// Unsupported get attribute operation
13        AttrUnsupported(typename: &'static str) {
14            description("object doesn't support getting attribute `a.b`")
15            display("object {} doesn't support getting attribute", typename)
16        }
17        /// No suche attribute on this object
18        AttrNotFound {
19            description("object doesn't have such attibute")
20        }
21        /// Unsupported subscription operation
22        IndexUnsupported(typename: &'static str) {
23            description("object doesn't support subscription `a[b]`")
24            display("object {} doesn't support subscription", typename)
25        }
26        /// Unsupported using this object as a key in dictionary subscription
27        StrKeyUnsupported(typename: &'static str) {
28            description("can't be stringified for subsciption `a[b]`")
29            display("object {} can't be stringified to be used as key", typename)
30        }
31        /// Unsupported using this object as a key in array subscription
32        IntKeyUnsupported(typename: &'static str) {
33            description("can't used as integer key for subscription")
34            display("object {} can't be a key for subscription", typename)
35        }
36        /// No such index on this object
37        IndexNotFound {
38            description("object doesn't have value at specified index")
39        }
40        /// The object can't be output
41        OutputUnsupported(typename: &'static str) {
42            description("can't print object of this type")
43            display("can't print object of type {}", typename)
44        }
45        /// The object can't be output
46        OutputError(typename: &'static str) {
47            description("error when formatting value")
48            display("error when formatting value of type {}", typename)
49        }
50        /// Named validator is not known
51        UnknownValidator(name: String) {
52            description("unknown validator")
53            display("validator {:?} is not defined", name)
54        }
55        /// Output did not match regex
56        RegexValidationError(data: String, regex: String) {
57            description("validation error")
58            display("output {:?} should match regex {:?}", data, regex)
59        }
60        /// The object can't be boolean
61        BoolUnsupported(typename: &'static str) {
62            description("can't treat object of this type as bool")
63            display("can't treat object of type {} as bool", typename)
64        }
65        /// The object can't be a number
66        NumberUnsupported(typename: &'static str) {
67            description("can't treat object of this type as number")
68            display("can't treat object of type {} as number", typename)
69        }
70        /// The object can't be compared to
71        ComparisonUnsupported(typename: &'static str) {
72            description("can't compare objects of this type")
73            display("can't compare objects of type {}", typename)
74        }
75        /// The object can't be iterated over
76        IterationUnsupported(typename: &'static str) {
77            description("can't iterate over the object")
78            display("can't iterate over the object of type {}", typename)
79        }
80        /// The object can't be iterated over by pairs
81        PairIterationUnsupported(typename: &'static str) {
82            description("can't iterate over the object by pairs")
83            display("can't iterate over the object by pairs of type {}",
84                    typename)
85        }
86        /// Variable or attribute not found
87        VariableNotFound(name: String) {
88            description("variable or attribute not found")
89            display("variable or attribute {:?} not found", name)
90        }
91        /// Incomparable types
92        Incomparable(left_type: &'static str, right_type: &'static str) {
93            description("two types can't be compared")
94            display("Can't compare object of type {:?} to {:?}",
95                left_type, right_type)
96        }
97        /// Custom error
98        Custom(err: Box<Error>) {
99            description(err.description())
100            display("{}", err)
101            cause(&**err)
102        }
103        #[doc(hidden)]
104        __Nonexhaustive
105    }
106}
107
108
109quick_error! {
110    /// Error rendering template
111    #[derive(Debug)]
112    pub enum RenderError {
113        /// Error writing into output buffer
114        Io(err: io::Error) {
115            display("I/O error: {}", err)
116            description("I/O error")
117            from()
118        }
119        /// Error formatting value
120        ///
121        /// TODO(tailhook) move it to the list of errors
122        Fmt(err: fmt::Error) {
123            description("error formatting value")
124            from()
125        }
126        /// Error when some of the variable has unexpected type or does
127        /// not support required operation
128        ///
129        /// When this kind of error occurs we try to skip error and do our
130        /// best to continue rendering and collect more errors
131        Data(errs: Vec<(Pos, DataError)>) {
132            display("data error: {}", errs.iter()
133                .map(|&(p, ref e)| format!("{}: {}", p, e))
134                .collect::<Vec<_>>().join("\n  "))
135            description("data error")
136        }
137    }
138}