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
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 a Grafbase directory")]
CreateGrafbaseDirectory,
#[error("could not create a schema.graphql file")]
WriteSchema,
#[error("could not delete the .grafbase directory\ncaused by: {0}")]
DeleteDotGrafbaseDirectory(io::Error),
}
impl ToExitCode for BackendError {
fn to_exit_code(&self) -> i32 {
match &self {
Self::AvailablePort | Self::PortInUse(_) => exitcode::UNAVAILABLE,
Self::AlreadyAProject(_) | Self::ProjectDirectoryExists(_) => exitcode::USAGE,
Self::ReadCurrentDirectory
| Self::CreateGrafbaseDirectory
| Self::WriteSchema
| Self::DeleteDotGrafbaseDirectory(_) => exitcode::DATAERR,
Self::ServerError(inner) => inner.to_exit_code(),
}
}
}