Improved plex error responses back from api
This commit is contained in:
@@ -4,42 +4,36 @@ import UserRepository from "../../../user/userRepository";
|
||||
const userRepository = new UserRepository();
|
||||
|
||||
class PlexAuthenticationError extends Error {
|
||||
constructor(errorResponse, statusCode) {
|
||||
constructor(errorResponse) {
|
||||
const message =
|
||||
"Unexptected error while authenticating to plex signin api. View error response.";
|
||||
super(message);
|
||||
|
||||
this.errorResponse = errorResponse;
|
||||
this.statusCode = statusCode;
|
||||
this.statusCode = 500;
|
||||
this.success = false;
|
||||
this.source = "plex";
|
||||
}
|
||||
}
|
||||
|
||||
function handleError(error, res) {
|
||||
const status = error?.status;
|
||||
let { message, source } = error;
|
||||
class PlexUnauthorizedError extends Error {
|
||||
constructor(errorResponse) {
|
||||
const message = "Unauthorized. Please check plex credentials.";
|
||||
super(message);
|
||||
|
||||
if (status && message) {
|
||||
if (status === 401) {
|
||||
message = "Unauthorized. Please check plex credentials.";
|
||||
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
|
||||
});
|
||||
this.errorResponse = errorResponse;
|
||||
this.statusCode = 401;
|
||||
this.success = false;
|
||||
this.source = "plex";
|
||||
}
|
||||
}
|
||||
|
||||
function handleResponse(response) {
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user