Skip to content

Commit

Permalink
Error codes as enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Mukhopadhyay committed Jul 7, 2023
1 parent f31fb2d commit 3dada33
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
18 changes: 7 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::io::Write;
use std::{env, fs, io, process};

use minigraf::{parse_query, parse_schema};
use server::{error_codes, logger};
use server::error_codes::ErrorCode;
use server::logger;

mod server;


fn main() {
// Get the schema file from the command line arguments or use the default.
let default_schema_file = "schema.graphql".to_string();
Expand All @@ -16,20 +16,16 @@ fn main() {
// Read the schema file and parse it into a schema object.
let schema_file = fs::read_to_string(schema_file).unwrap_or_else(|err| {
logger::error_log(
error_codes::ERROR_INVALID_SCHEMA_FILE,
ErrorCode::InvalidSchemaFile,
"Couldn't read schema file",
err,
);
process::exit(error_codes::ERROR_INVALID_SCHEMA_FILE);
process::exit(ErrorCode::InvalidSchemaFile as i32);
});

let schema = parse_schema::<String>(&schema_file).unwrap_or_else(|err| {
logger::error_log(
error_codes::ERROR_INVALID_SCHEMA,
"Couldn't parse schema",
err,
);
process::exit(error_codes::ERROR_INVALID_SCHEMA);
logger::error_log(ErrorCode::InvalidSchema, "Couldn't parse schema", err);
process::exit(ErrorCode::InvalidSchema as i32);
});
logger::debug_log("schema", &schema);

Expand All @@ -48,7 +44,7 @@ fn main() {
let query = parse_query::<String>(query);
if query.is_err() {
logger::error_log(
error_codes::ERROR_BAD_QUERY,
ErrorCode::BadQuery,
"Couldn't parse query",
query.err().unwrap(),
);
Expand Down
21 changes: 18 additions & 3 deletions src/server/error_codes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
pub const ERROR_INVALID_SCHEMA_FILE: i32 = 1;
pub const ERROR_INVALID_SCHEMA: i32 = 2;
pub const ERROR_BAD_QUERY: i32 = 3;
use std::fmt::Display;

pub enum ErrorCode {
InvalidSchemaFile = 1,
InvalidSchema = 2,
BadQuery = 3,
}

impl Display for ErrorCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let error_code = match self {
ErrorCode::InvalidSchemaFile => "InvalidSchemaFile",
ErrorCode::InvalidSchema => "InvalidSchema",
ErrorCode::BadQuery => "BadQuery",
};
write!(f, "{}", error_code)
}
}
3 changes: 2 additions & 1 deletion src/server/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::{Debug, Display};
use std::str::FromStr;

use lazy_static::lazy_static;
use crate::server::error_codes::ErrorCode;

#[derive(PartialEq, PartialOrd)]
pub enum LogLevel {
Expand Down Expand Up @@ -35,7 +36,7 @@ lazy_static! {
};
}

pub fn error_log<E>(error_code: i32, message: &str, err: E)
pub fn error_log<E>(error_code: ErrorCode, message: &str, err: E)
where
E: Display,
{
Expand Down

0 comments on commit 3dada33

Please sign in to comment.