-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.rs
114 lines (88 loc) · 3 KB
/
error.rs
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
use crate::git::ConfigError;
use crate::shellquote::SplitError;
use chipp_http::Error as HttpError;
use git2::Error as GitError;
use std::error::Error as StdError;
use std::io::Error as IoError;
#[derive(Debug)]
pub enum Error {
AuthorizationError,
InvalidRepo,
Detached,
GetConfig(ConfigError),
InvalidAlias(String, SplitError),
Git(GitError),
Http(HttpError),
OpenUrl(IoError, url::Url),
JiraUrlNotConfigured,
NoJiraTicket(String),
NoPrsForBranch(String, HttpError),
NoPrWithId(u16, HttpError),
NotInWorkTree,
RepoExistsAndPublic(String),
RemoteExists(String, String),
FailedToExecuteGit(IoError),
}
impl From<GitError> for Error {
fn from(err: GitError) -> Error {
Error::Git(err)
}
}
impl From<HttpError> for Error {
fn from(err: HttpError) -> Error {
Error::Http(err)
}
}
impl From<ConfigError> for Error {
fn from(err: ConfigError) -> Self {
Error::GetConfig(err)
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
use Error::*;
match self {
Git(err) => Some(err),
Http(err) => Some(err),
OpenUrl(err, _) => Some(err),
NoPrsForBranch(_, err) => Some(err),
NoPrWithId(_, err) => Some(err),
GetConfig(err) => Some(err),
InvalidAlias(_, err) => Some(err),
FailedToExecuteGit(err) => Some(err),
_ => None,
}
}
}
use std::fmt;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Error::*;
match self {
AuthorizationError => write!(f, "token is invalid"),
InvalidRepo => write!(f, "this is not a bitbucket repository"),
Detached => write!(f, "can't find the current branch"),
GetConfig(err) => write!(f, "{}", err),
InvalidAlias(alias, _) => write!(f, "invalid alias for `{alias}`"),
Git(err) => write!(f, "{}", err),
Http(err) => write!(f, "{}", err),
OpenUrl(err, url) => write!(f, "can't open URL {}: {}", url, err),
JiraUrlNotConfigured => write!(f, "JIRA url is not specified in .git/config"),
NoJiraTicket(branch) => {
write!(f, "can't find JIRA ticket in branch name `{}`", branch)
}
NoPrsForBranch(branch, err) => {
write!(f, "can't find prs for branch {}: {}", branch, err)
}
NoPrWithId(id, err) => write!(f, "can't find pr with id {}: {}", id, err),
RepoExistsAndPublic(repo) => {
write!(f, "repo `{repo}` already exists and is public")
}
RemoteExists(remote, url) => {
write!(f, "remote `{remote}` already exists with url `{url}`")
}
NotInWorkTree => write!(f, "not in a git repository"),
FailedToExecuteGit(err) => write!(f, "failed to execute git: {}", err),
}
}
}