refactor(api): use HTTP status codes to convey API call results

This commit is contained in:
Philipp Matthias Schaefer 2021-03-14 21:34:32 +01:00
parent 0d1cc9ae54
commit adb2cdfc41
2 changed files with 43 additions and 61 deletions

View File

@ -19,10 +19,11 @@ use handlebars::Handlebars;
use ldap3::{ldap_escape, LdapConn}; use ldap3::{ldap_escape, LdapConn};
use ldap3::exop::PasswordModify; use ldap3::exop::PasswordModify;
use ldap3::result::LdapError; use ldap3::result::LdapError;
use rocket::http::Status;
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
use rocket::post; use rocket::post;
use serde_derive::{Deserialize, Serialize}; use serde_derive::Deserialize;
use serde_json::json; use serde_json::json;
use crate::context::Context; use crate::context::Context;
@ -34,18 +35,6 @@ pub struct PasswordData {
new_password: String, new_password: String,
} }
#[derive(Serialize)]
enum Message {
InvalidCredentials,
ServerError,
}
#[derive(Serialize)]
pub struct Response {
success: bool,
message: Option<Message>,
}
fn change_password(data: &Json<PasswordData>, fn change_password(data: &Json<PasswordData>,
context: &rocket::State<Context>) -> Result<()> { context: &rocket::State<Context>) -> Result<()> {
@ -80,33 +69,23 @@ fn change_password(data: &Json<PasswordData>,
#[post("/update", data = "<data>")] #[post("/update", data = "<data>")]
pub fn update(data: Json<PasswordData>, pub fn update(data: Json<PasswordData>,
context: rocket::State<Context>) -> Json<Response> { context: rocket::State<Context>) -> Status {
Json( match change_password(&data, &context) {
match change_password(&data, &context) { Ok(_) => Status::Accepted,
Ok(_) => Response{ Err(error) => {
success: true, eprintln!("LDAP error: {}", error);
message: None, match error.downcast::<LdapError>().expect(
}, "No other error should occur here"
Err(error) => { ) {
eprintln!("LDAP error: {}", error); LdapError::LdapResult{ result } => {
Response { if result.rc == 49 {
success: false, Status::Unauthorized
message: Some( } else {
match error.downcast::<LdapError>().expect( Status::InternalServerError
"No other error should occur here" }
) { },
LdapError::LdapResult{ result } => { _ => Status::InternalServerError,
if result.rc == 49 { }
Message::InvalidCredentials },
} else { }
Message::ServerError
}
},
_ => Message::ServerError,
}
),
}
},
}
)
} }

View File

@ -27,9 +27,8 @@ MODAL_STATES = {
}, },
"FetchError" : { "FetchError" : {
button_disabled : false, button_disabled : false,
message : function(parameters) { message : "An error occurred while contacting the server",
`An error occurred while contacting the server (${parameters.statusCode})` icon_url : "cross.svg",
},
}, },
"ServerError" : { "ServerError" : {
button_disabled : false, button_disabled : false,
@ -43,7 +42,7 @@ MODAL_STATES = {
}, },
} }
function showModal(state_name, parameters) { function showModal(state_name) {
const state = MODAL_STATES[state_name]; const state = MODAL_STATES[state_name];
const message = document.getElementById("message"); const message = document.getElementById("message");
@ -52,9 +51,7 @@ function showModal(state_name, parameters) {
img.src = state.icon_url; img.src = state.icon_url;
const text = message.childNodes[1]; const text = message.childNodes[1];
text.textContent = " " + (state.message instanceof Function text.textContent = " " + state.message;
? state.message(parameters)
: state.message);
const button = document.getElementById("close_button"); const button = document.getElementById("close_button");
button.disabled = state.button_disabled; button.disabled = state.button_disabled;
@ -97,17 +94,23 @@ function changePasswords() {
method: "POST", method: "POST",
body: JSON.stringify(query), body: JSON.stringify(query),
}).then(response => { }).then(response => {
if(response.status == 200) { switch(response.status) {
return response.json(); case 202:
} else { showModal("Success");
showModal("FetchError", { statusCode : response.status }); break;
case 401:
showModal("InvalidCredentials");
break;
case 500:
showModal("ServerError");
break;
default:
console.log("Unexpected status code: " + response.status);
showModal("FetchError");
break;
} }
}).then(response => { }, error => {
if(response.success) { console.log("Fetch error: " + error);
showModal("Success"); showModal("FetchError");
clearFields(); })
} else {
showModal(response.message);
}
});
} }