hacker_news/
error.rs

1use std::error::Error;
2use std::fmt::Display;
3use std::fmt;
4
5#[derive(Debug)]
6pub struct HttpError {
7    pub code: u16,
8    pub url: String,
9}
10
11#[derive(Debug)]
12pub enum HnError {
13    // Error used when parsing of an HTML document fails
14    HtmlParsingError,
15    // Error used when program attempts to invoke an action requiring authentication,
16    // but is not authenticated
17    UnauthenticatedError,
18    // Error used when a client fails to authenticate
19    AuthenticationError,
20    // Error raised from a failure during an HTTP request/response 
21    HttpError(HttpError),
22}
23
24impl Display for HnError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            HnError::HtmlParsingError => {
28                write!(f, "HtmlParsingErr: There was a problem parsing HTML data. This is an internal library error.")
29            },
30            HnError::UnauthenticatedError => {
31                write!(f, "UnauthenticatedError: An unauthenticated client attempted an action requiring authorization.")
32            }
33            HnError::AuthenticationError => {
34                write!(f, "A client failed to authenticate. Please check credential information, and authentication frequency")
35            }
36            HnError::HttpError(http_err) => {
37                write!(f, "A client failed during an HTTP request/response; url '{}', code '{}'",
38                    http_err.url,
39                    http_err.code,
40                )
41            }
42        }
43    }
44}
45
46
47impl Error for HnError {}