Make all api follow the same error pattern

This commit is contained in:
2025-07-17 18:25:50 -07:00
parent 792e0abba3
commit 91b39504b6
@@ -47,7 +47,7 @@ namespace BoredCareers.Controllers {
IsPersistent = StayLoggedIn, // Is set from the StayLoggedIn
}
);
return test;
return Ok(test);
} else {
test.CurrentPasswordAttempts += 1;
await _databaseService.SetAccount(test);
@@ -61,7 +61,7 @@ namespace BoredCareers.Controllers {
return NotFound("Account Not Found");
} catch (Exception ex) {
Console.WriteLine("Login Error: " + ex.Message);
return NotFound();
return NotFound("An internal server error has occured");
}
}
@@ -110,10 +110,10 @@ namespace BoredCareers.Controllers {
return Ok();
}
}
return NotFound();
return NotFound("Not logged in");
} catch (Exception ex) {
Console.WriteLine("ChangePassword Error: " + ex.Message);
return NotFound();
return NotFound("An internal server error has occured");
}
}
@@ -128,10 +128,10 @@ namespace BoredCareers.Controllers {
await _databaseService.SetAccount(user);
return Ok();
}
return NotFound();
return NotFound("Not logged in");
} catch (Exception ex) {
Console.WriteLine("ToggleAccountLock Error: " + ex.Message);
return NotFound();
return NotFound("An internal server error has occured");
}
}
@@ -142,10 +142,10 @@ namespace BoredCareers.Controllers {
if (isLoggedIn()) {
return Ok(await getLoggedInUser());
}
return NotFound();
return NotFound("Not logged in");
} catch (Exception ex) {
Console.WriteLine("Get Error: " + ex);
return NotFound();
return NotFound("An internal server error has occured");
}
}
@@ -168,7 +168,7 @@ namespace BoredCareers.Controllers {
if (_emailContext._SentEmails.ContainsKey(key)) {
DateTime PreviousSentTime = _emailContext._SentEmails.GetValueOrDefault(key);
if (PreviousSentTime.AddMinutes(5) > DateTime.Now) {
return "Cannot sent another verify email until 5 minutes has elapsed ";
return NotFound("Cannot sent another verify email until 5 minutes has elapsed");
}
else {
_emailContext._SentEmails.Remove(key);
@@ -186,11 +186,11 @@ namespace BoredCareers.Controllers {
string result = _emailContext.Send(test.Email, EmailService.VerifyEmailSubject, EmailContents);
_emailContext._SentEmails.Add(key, DateTime.Now);
return result;
return Ok(result);
}
return "Account not found";
return NotFound("Account not found");
} catch (Exception) {
return "The connection couldn't be established to the email server";
return NotFound("An internal server error has occured");
}
}
@@ -204,12 +204,12 @@ namespace BoredCareers.Controllers {
test.EmailToken = "";
test.EmailVerified = true;
await _databaseService.SetAccount(test);
return true;
return Ok(true);
}
}
return false;
return NotFound("Account not found or token is invalid");;
} catch {
return false;
return NotFound("An internal server error has occured");
}
}
@@ -222,7 +222,7 @@ namespace BoredCareers.Controllers {
if (_emailContext._SentEmails.ContainsKey(key)) {
DateTime PreviousSentTime = _emailContext._SentEmails.GetValueOrDefault(key);
if (PreviousSentTime.AddMinutes(5) > DateTime.Now) {
return "Cannot sent another reset requests until 5 minutes has elapsed";
return NotFound("Cannot sent another reset requests until 5 minutes has elapsed");
}
else {
_emailContext._SentEmails.Remove(key);
@@ -240,12 +240,12 @@ namespace BoredCareers.Controllers {
string result = _emailContext.Send(test.Email, EmailService.VerifyEmailSubject, EmailContents);
_emailContext._SentEmails.Add(key, DateTime.Now);
return result;
return Ok(result);
}
return "Account Not Found";
return NotFound("Account Not Found");
} catch (Exception e) {
Console.WriteLine("EmailService Error: " + e.ToString());
return "The connection couldn't be established to the email server";
return NotFound("An internal server error has occured");
}
}
@@ -261,12 +261,12 @@ namespace BoredCareers.Controllers {
test.EmailToken = "";
test.PasswordHash = BCrypt.Net.BCrypt.HashPassword(NewPassword);
await _databaseService.SetAccount(test);
return true;
return Ok(true);
}
}
return false;
return NotFound("Account not found or reset token is bad");
} catch {
return false;
return NotFound("An internal server error has occured");
}
}
@@ -281,10 +281,10 @@ namespace BoredCareers.Controllers {
return Ok();
}
}
return NotFound();
return NotFound("User is not logged in");
} catch (Exception ex) {
Console.WriteLine("Delete Error: " + ex.Message);
return NotFound();
return NotFound("An internal server error has occured");
}
}