Improved plex error responses back from api

This commit is contained in:
2022-08-22 23:37:53 +02:00
parent ce45ed7b7f
commit 79397ccf1f

View File

@@ -4,42 +4,36 @@ import UserRepository from "../../../user/userRepository";
const userRepository = new UserRepository(); const userRepository = new UserRepository();
class PlexAuthenticationError extends Error { class PlexAuthenticationError extends Error {
constructor(errorResponse, statusCode) { constructor(errorResponse) {
const message = const message =
"Unexptected error while authenticating to plex signin api. View error response."; "Unexptected error while authenticating to plex signin api. View error response.";
super(message); super(message);
this.errorResponse = errorResponse; this.errorResponse = errorResponse;
this.statusCode = statusCode; this.statusCode = 500;
this.success = false; this.success = false;
this.source = "plex";
} }
} }
function handleError(error, res) { class PlexUnauthorizedError extends Error {
const status = error?.status; constructor(errorResponse) {
let { message, source } = error; const message = "Unauthorized. Please check plex credentials.";
super(message);
if (status && message) { this.errorResponse = errorResponse;
if (status === 401) { this.statusCode = 401;
message = "Unauthorized. Please check plex credentials."; this.success = false;
source = "plex"; this.source = "plex";
}
res.status(status).send({ success: false, message, source });
} else {
// eslint-disable-next-line no-console
console.log("caught authenticate plex account controller error", error);
res.status(500).send({
message:
"An unexpected error occured while authenticating your account with plex",
source
});
} }
} }
function handleResponse(response) { function handleResponse(response) {
if (!response.ok) { if (!response.ok) {
throw new PlexAuthenticationError(response.statusText, response.status); if (response?.status === 401)
throw new PlexUnauthorizedError(response?.statusText);
throw new PlexAuthenticationError(response?.statusText);
} }
return response.json(); return response.json();