refactor(api): use HTTP status codes to convey API call results
This commit is contained in:
parent
0d1cc9ae54
commit
adb2cdfc41
63
src/api.rs
63
src/api.rs
@ -19,10 +19,11 @@ use handlebars::Handlebars;
|
||||
use ldap3::{ldap_escape, LdapConn};
|
||||
use ldap3::exop::PasswordModify;
|
||||
use ldap3::result::LdapError;
|
||||
use rocket::http::Status;
|
||||
use rocket_contrib::json::Json;
|
||||
|
||||
use rocket::post;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use serde_derive::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::context::Context;
|
||||
@ -34,18 +35,6 @@ pub struct PasswordData {
|
||||
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>,
|
||||
context: &rocket::State<Context>) -> Result<()> {
|
||||
|
||||
@ -80,33 +69,23 @@ fn change_password(data: &Json<PasswordData>,
|
||||
|
||||
#[post("/update", data = "<data>")]
|
||||
pub fn update(data: Json<PasswordData>,
|
||||
context: rocket::State<Context>) -> Json<Response> {
|
||||
Json(
|
||||
match change_password(&data, &context) {
|
||||
Ok(_) => Response{
|
||||
success: true,
|
||||
message: None,
|
||||
},
|
||||
Err(error) => {
|
||||
eprintln!("LDAP error: {}", error);
|
||||
Response {
|
||||
success: false,
|
||||
message: Some(
|
||||
match error.downcast::<LdapError>().expect(
|
||||
"No other error should occur here"
|
||||
) {
|
||||
LdapError::LdapResult{ result } => {
|
||||
if result.rc == 49 {
|
||||
Message::InvalidCredentials
|
||||
} else {
|
||||
Message::ServerError
|
||||
}
|
||||
},
|
||||
_ => Message::ServerError,
|
||||
}
|
||||
),
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
context: rocket::State<Context>) -> Status {
|
||||
match change_password(&data, &context) {
|
||||
Ok(_) => Status::Accepted,
|
||||
Err(error) => {
|
||||
eprintln!("LDAP error: {}", error);
|
||||
match error.downcast::<LdapError>().expect(
|
||||
"No other error should occur here"
|
||||
) {
|
||||
LdapError::LdapResult{ result } => {
|
||||
if result.rc == 49 {
|
||||
Status::Unauthorized
|
||||
} else {
|
||||
Status::InternalServerError
|
||||
}
|
||||
},
|
||||
_ => Status::InternalServerError,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -27,9 +27,8 @@ MODAL_STATES = {
|
||||
},
|
||||
"FetchError" : {
|
||||
button_disabled : false,
|
||||
message : function(parameters) {
|
||||
`An error occurred while contacting the server (${parameters.statusCode})`
|
||||
},
|
||||
message : "An error occurred while contacting the server",
|
||||
icon_url : "cross.svg",
|
||||
},
|
||||
"ServerError" : {
|
||||
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 message = document.getElementById("message");
|
||||
@ -52,9 +51,7 @@ function showModal(state_name, parameters) {
|
||||
img.src = state.icon_url;
|
||||
|
||||
const text = message.childNodes[1];
|
||||
text.textContent = " " + (state.message instanceof Function
|
||||
? state.message(parameters)
|
||||
: state.message);
|
||||
text.textContent = " " + state.message;
|
||||
|
||||
const button = document.getElementById("close_button");
|
||||
button.disabled = state.button_disabled;
|
||||
@ -97,17 +94,23 @@ function changePasswords() {
|
||||
method: "POST",
|
||||
body: JSON.stringify(query),
|
||||
}).then(response => {
|
||||
if(response.status == 200) {
|
||||
return response.json();
|
||||
} else {
|
||||
showModal("FetchError", { statusCode : response.status });
|
||||
switch(response.status) {
|
||||
case 202:
|
||||
showModal("Success");
|
||||
break;
|
||||
case 401:
|
||||
showModal("InvalidCredentials");
|
||||
break;
|
||||
case 500:
|
||||
showModal("ServerError");
|
||||
break;
|
||||
default:
|
||||
console.log("Unexpected status code: " + response.status);
|
||||
showModal("FetchError");
|
||||
break;
|
||||
}
|
||||
}).then(response => {
|
||||
if(response.success) {
|
||||
showModal("Success");
|
||||
clearFields();
|
||||
} else {
|
||||
showModal(response.message);
|
||||
}
|
||||
});
|
||||
}, error => {
|
||||
console.log("Fetch error: " + error);
|
||||
showModal("FetchError");
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user