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
135
136
137
use common::traits::ToExitCode;
use std::{io, path::PathBuf};
use thiserror::Error;
pub use server::errors::ServerError;
#[derive(Error, Debug)]
pub enum BackendError {
#[error("could not find an available port")]
AvailablePort,
#[error("port {0} is currently in use")]
PortInUse(u16),
#[error(transparent)]
ServerError(ServerError),
#[error("{0} already exists")]
AlreadyAProject(PathBuf),
#[error("{0} already exists")]
ProjectDirectoryExists(PathBuf),
#[error("could not read the current path")]
ReadCurrentDirectory,
#[error("could not create the 'grafbase' directory\ncaused by: {0}")]
CreateGrafbaseDirectory(io::Error),
#[error("could not create the project directory\ncaused by: {0}")]
CreateProjectDirectory(io::Error),
#[error("could not create a schema.graphql file\ncaused by: {0}")]
WriteSchema(io::Error),
#[error("could not delete the .grafbase directory\ncaused by: {0}")]
DeleteDotGrafbaseDirectory(io::Error),
#[error("could not delete the grafbase directory\ncaused by: {0}")]
DeleteGrafbaseDirectory(io::Error),
#[error("'{0}' is not a supported template URL")]
UnsupportedTemplateURL(String),
#[error("'{0}' is not a valid URL")]
MalformedTemplateURL(String),
#[error("could not download the archive for '{0}'\ncaused by: {1}")]
StartDownloadRepoArchive(String, reqwest_middleware::Error),
#[error("could not download the archive for '{0}'")]
DownloadRepoArchive(String),
#[error("could not find the provided template within the template repository")]
TemplateNotFound,
#[error("could not move the extracted files from the template repository\ncaused by: {0}")]
MoveExtractedFiles(io::Error),
#[error("could not read the entries of the template repository archive")]
ReadArchiveEntries,
#[error("could not extract an entry from the template repository archive\ncaused by: {0}")]
ExtractArchiveEntry(io::Error),
#[error("could not clean the files extracted from the repository archive\ncaused by: {0}")]
CleanExtractedFiles(io::Error),
#[error("could not get the repository information for {0}")]
StartGetRepositoryInformation(String),
#[error("could not get the repository information for {0}")]
GetRepositoryInformation(String),
#[error("could not read the repository information for {0}")]
ReadRepositoryInformation(String),
}
impl ToExitCode for BackendError {
fn to_exit_code(&self) -> i32 {
match &self {
Self::AvailablePort
| Self::PortInUse(_)
| Self::StartDownloadRepoArchive(_, _)
| Self::StartGetRepositoryInformation(_)
| Self::ReadRepositoryInformation(_)
| Self::DownloadRepoArchive(_)
| Self::ReadArchiveEntries
| Self::GetRepositoryInformation(_) => exitcode::UNAVAILABLE,
Self::AlreadyAProject(_) | Self::ProjectDirectoryExists(_) => exitcode::USAGE,
Self::UnsupportedTemplateURL(_) | Self::MalformedTemplateURL(_) | Self::TemplateNotFound => {
exitcode::DATAERR
}
Self::ReadCurrentDirectory
| Self::MoveExtractedFiles(_)
| Self::DeleteDotGrafbaseDirectory(_)
| Self::DeleteGrafbaseDirectory(_)
| Self::WriteSchema(_)
| Self::CreateGrafbaseDirectory(_)
| Self::ExtractArchiveEntry(_)
| Self::CleanExtractedFiles(_)
| Self::CreateProjectDirectory(_) => exitcode::IOERR,
Self::ServerError(inner) => inner.to_exit_code(),
}
}
}