-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca69585
commit df1f72e
Showing
7 changed files
with
67 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "k8s-job-dispatcher" | ||
version = "0.4.0" | ||
version = "0.4.1" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! Provides an error type for API responses. | ||
use actix_web::{http::StatusCode, HttpResponse, ResponseError}; | ||
use serde::Serialize; | ||
use serde_json::{json, to_string_pretty}; | ||
use std::fmt::{Display, Formatter, Result}; | ||
|
||
/// An error serialized as JSON and sent as a response. | ||
#[derive(Debug, Serialize)] | ||
pub struct APIError { | ||
status: u16, | ||
msg: String, | ||
} | ||
|
||
impl APIError { | ||
fn new<S: ToString>(status: u16, msg: S) -> Self { | ||
Self { | ||
status, | ||
msg: msg.to_string(), | ||
} | ||
} | ||
|
||
pub fn bad_request<S: ToString>(msg: S) -> Self { | ||
Self::new(400, msg) | ||
} | ||
|
||
pub fn bad_gateway<S: ToString>(msg: S) -> Self { | ||
Self::new(502, msg) | ||
} | ||
|
||
pub fn not_found<S: ToString>(msg: S) -> Self { | ||
Self::new(404, msg) | ||
} | ||
} | ||
|
||
impl Display for APIError { | ||
fn fmt(&self, f: &mut Formatter) -> Result { | ||
write!(f, "{}", to_string_pretty(self).unwrap()) | ||
} | ||
} | ||
|
||
impl ResponseError for APIError { | ||
fn error_response(&self) -> HttpResponse { | ||
let err_json = json!({ "error": { "code": self.status, "message": self.msg }}); | ||
HttpResponse::build(StatusCode::from_u16(self.status).unwrap()).json(err_json) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters