grp_core/error/structs.rs
1use color_print::cformat;
2
3use crate::error::types::ErrorType;
4
5/// # Error
6/// This is the _error struct_ used in all the lib
7/// it implements some based functionality to show the error
8/// in a beautifull format.
9///
10/// There are to basic ways to create a new error,
11/// with a preset and additional information, or a custom, for
12/// your own error.
13///
14/// ## Example 1
15/// ~~~
16/// use grp_core::{ErrorType, Error};
17///
18/// Error::new(
19/// ErrorType::ResponseParsing,
20/// vec!["description", "{}"]
21/// );
22/// ~~~
23///
24/// Error contains a vector to emulate various parameter to allow a more
25/// usefull message to the user. In this case, the `ErrorType::ResponseParsing`
26/// needs at least 2 parameters, one for the description of the error,
27/// and other for the text that generates the error.
28///
29/// ## Example 2
30/// ~~~
31/// use grp_core::{ErrorType, Error};
32///
33/// Error::new_custom(
34/// "Custom error message",
35/// vec!["something whent wrong after...", "don't be afraid"] // here, every item is trated as a new line (if printed)
36/// );
37/// ~~~
38///
39/// You also can create custom errors using the _build in_ method _new_custom_
40/// this is usefull if you whant to canvert between error in your aplication,
41/// a good example is present in _https://github.com/feraxhp/grp_, where the
42/// errors from git2 are parsed to grp_core errors, in order to keep the
43/// same error throughout all the aplication.
44///
45/// ## Example 3
46/// ~~~
47/// use grp_core::{ErrorType, Error};
48///
49/// let normal_error = Error::new(
50/// ErrorType::ResponseParsing,
51/// vec!["description", "{}"]
52/// );
53///
54/// let error_custom = Error::new_custom(
55/// "Custom error message",
56/// vec!["something whent wrong after...", "don't be afraid"] // here, every item is trated as a new line (if printed)
57/// );
58///
59/// let collection_error = Error::colection(vec![normal_error, error_custom]);
60/// ~~~
61///
62/// There is a 3 type and is a collection, this allows to return multiple errors
63/// in just one `grp_core::Error`. usefull for paggination errors, or concations of multiple
64/// of them.
65#[derive(Debug)]
66pub struct Error {
67 pub message: String,
68 pub content: Vec<String>,
69}
70
71#[allow(dead_code)]
72impl Error {
73 pub fn new<T: Into<String>>(error: ErrorType, content: Vec<T>) -> Error {
74 Error {
75 message: error.get_message(),
76 content: error.map_content(content.into_iter().map(|s| s.into()).collect()),
77 }
78 }
79
80 pub fn colection(errors: Vec<Error>) -> Error {
81 let mut content = Vec::new();
82 for error in errors {
83 content.push(cformat!("\n*<r>{}</>",error.message));
84 content.extend(error.content.iter().map(|s| format!(" {}", s)));
85 }
86 Error {
87 message: "Multiple errors found".to_string(),
88 content: content
89 }
90 }
91
92 pub fn new_custom<T: Into<String>>(message: T, content: Vec<T>) -> Error {
93 Error {
94 message: message.into(),
95 content: content.into_iter().map(|s| s.into()).collect()
96 }
97 }
98
99 pub fn show(&self) { self.show_with_offset(0); }
100
101 pub fn show_with_offset(&self, offset: usize) {
102 self.content.iter().for_each(|line| {
103 eprintln!("{:width$}{}", "", line, width = offset);
104 });
105 }
106}