about summary refs log tree commit diff stats
path: root/pkgs/by-name/ba/back/src/error/mod.rs
blob: 8889033f94fc76d2ec9177eb0aac15cfbf13108b (plain) (blame)
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
// Back - An extremely simple git issue tracking system. Inspired by tvix's
// panettone
//
// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This file is part of Back.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/agpl.txt>.

use std::{fmt::Display, io, net::SocketAddr, path::PathBuf};

use gix::hash::Prefix;
use thiserror::Error;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Error, Debug)]
pub enum Error {
    ConfigParse {
        file: PathBuf,
        error: serde_json::Error,
    },

    ProjectListRead {
        file: PathBuf,
        error: io::Error,
    },
    ConfigRead {
        file: PathBuf,
        error: io::Error,
    },
    NotGitBug {
        path: PathBuf,
    },
    RepoOpen {
        repository_path: PathBuf,
        error: Box<gix::open::Error>,
    },
    RepoFind {
        repository_path: PathBuf,
    },
    RepoRefsIter(#[from] gix::refs::packed::buffer::open::Error),
    RepoRefsPrefixed {
        error: io::Error,
    },

    TcpBind {
        addr: SocketAddr,
        err: io::Error,
    },
    TcpAccept {
        err: io::Error,
    },

    IssuesPrefixMissing {
        prefix: Prefix,
    },
    IssuesPrefixParse(#[from] gix::hash::prefix::from_hex::Error),
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::ConfigParse { file, error } => {
                write!(
                    f,
                    "while trying to parse the config file ({}): {error}",
                    file.display()
                )
            }
            Error::ProjectListRead { file, error } => {
                write!(
                    f,
                    "while trying to read the project.list file ({}): {error}",
                    file.display()
                )
            }
            Error::ConfigRead { file, error } => {
                write!(
                    f,
                    "while trying to read the config file ({}): {error}",
                    file.display()
                )
            }
            Error::RepoOpen {
                repository_path,
                error,
            } => {
                write!(
                    f,
                    "while trying to open the repository ({}): {error}",
                    repository_path.display()
                )
            }
            Error::NotGitBug { path } => {
                write!(
                    f,
                    "Repository ('{}') has no initialized git-bug data",
                    path.display()
                )
            }
            Error::RepoFind { repository_path } => {
                write!(
                    f,
                    "failed to find the repository at path: '{}'",
                    repository_path.display()
                )
            }
            Error::RepoRefsIter(error) => {
                write!(f, "while iteration over the refs in a repository: {error}",)
            }
            Error::RepoRefsPrefixed { error, .. } => {
                write!(f, "while prefixing the refs with a path: {error}")
            }
            Error::IssuesPrefixMissing { prefix } => {
                write!(
                    f,
                    "There is no 'issue' associated with the prefix: {prefix}"
                )
            }
            Error::IssuesPrefixParse(error) => {
                write!(f, "The given prefix can not be parsed as prefix: {error}")
            }
            Error::TcpBind { addr, err } => {
                write!(f, "while trying to open tcp {addr} for listening: {err}.")
            }
            Error::TcpAccept { err } => {
                write!(f, "while trying to accept a tcp connection: {err}.")
            }
        }
    }
}