github_heatmap/utils/
errors.rs

1use thiserror::Error;
2use reqwest::StatusCode;
3
4/// A collection of error variants related to making a request
5/// to a Github profile page prior to scraping.
6#[derive(Error, Debug, Eq, PartialEq)]
7pub enum GithubError {
8    /// Represents a generic failure while trying to make a GET request
9    /// to the specified Github profile page.
10    #[error("Unable to reach Github services. Try again later!")]
11    BadRequest,
12    
13    /// Represents a failure caused while attempting to scrape the 
14    /// specified Github profile page. This is typically caused by either
15    /// a Github service outage, or any other kind of rejection (e.g rate-limiting, etc).
16    #[error("Unable to scrape Github profile: '{0}'")]
17    ScrapeFailure(StatusCode),
18
19    /// Represents a 404 response caused by attempting to scrape a non-existing
20    /// Github profile page.
21    #[error("Unable to find Github profile at url: '{0}'")]
22    ProfileNotFound(String),
23}
24
25/// A collection of error variants related to parsing a Github contribution
26/// heatmap.
27#[derive(Error, Debug, Eq, PartialEq)]
28pub enum HeatmapError {
29    /// Represents a failure to query an element in the scraped document.
30    /// This is usually caused by an update to the Github front end.
31    #[error("Failed to query element '{alias}' with selector: '{selector}'")]
32    QueryElement { 
33        /// User-friendly alias related to element.
34        alias: String, 
35        /// CSS Selector used while attempting to query element.
36        selector: String 
37    },
38
39    /// Represents a failure to select an attribute on an HTML Element. This
40    /// is usually caused by an update to the Github front end. 
41    #[error("Failed to query attribute '{attr}' on '{on_alias}'!")]
42    QueryAttribute { 
43        /// Attribute name used while attempting to select attribute.
44        attr: String, 
45        /// User-friendly alias related to element that attribute purportedly belongs to.
46        on_alias: String 
47    },
48
49    /// Represents a failure to parse an attribute on an HTML Element. For example,
50    /// attempting to parse a Rect element's y attribute as a usize.
51    /// This is usually caused by an update to the Github front end.
52    #[error("Failed to parse attribute '{attr}' on '{on_alias}'!")]
53    ParseAttribute { 
54        /// Attribute name used while attempting to parse attribute.
55        attr: String, 
56        /// User-friendly alias related to element that attribute purportedly belongs to.
57        on_alias: String 
58    },
59
60    /// Represents a failure to parse Heatmap node sizes from the SVG element belonging
61    /// to the Github profile page. Elements are typically distanced either 13px or 15px
62    /// depending on the density of the profile page (contains README.md, etc).
63    #[error("Failed to parse Heatmap nodes. Unknown node size scraped from Github frontend.")]
64    UnknownNodeFormat
65}