1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

fix: improve API error-handling (#1301)

Unleash is an API and it would simplyfy a lot of the specific
errors could carry the expected HTTP status code for this error.
This would eliminate the need for a gigantic switch/case in the
handle-errors function.
This commit is contained in:
Ivar Conradi Østhus 2022-01-26 13:45:22 +01:00 committed by GitHub
parent cd5f3bb964
commit a50d0e2a21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 12 deletions

View File

@ -19,5 +19,5 @@ class BadDataError extends Error {
};
}
}
export default BadDataError;
module.exports = BadDataError;

View File

@ -0,0 +1,26 @@
class BaseError extends Error {
readonly statusCode: number;
constructor(message: string, statusCode: number, name: string) {
super();
this.name = name;
this.message = message;
this.statusCode = statusCode;
Error.captureStackTrace(this, this.constructor);
}
toJSON(): object {
return {
isJoi: true,
name: this.constructor.name,
details: [
{
message: this.message,
},
],
};
}
}
export default BaseError;

View File

@ -0,0 +1,10 @@
import BaseError from './base-error';
class DisabledError extends BaseError {
constructor(message: string) {
super(message, 422, 'DisabledError');
Error.captureStackTrace(this, this.constructor);
}
}
export default DisabledError;

View File

@ -0,0 +1,10 @@
import BaseError from './base-error';
class PasswordMismatch extends BaseError {
constructor(message: string = 'Wrong password, try again.') {
super(message, 401, 'PasswordMismatch');
Error.captureStackTrace(this, this.constructor);
}
}
export default PasswordMismatch;

View File

@ -79,4 +79,3 @@ class ResetPasswordController extends Controller {
}
export default ResetPasswordController;
module.exports = ResetPasswordController;

View File

@ -18,13 +18,9 @@ class PasswordProvider extends Controller {
});
}
try {
const user = await this.userService.loginUser(username, password);
req.session.user = user;
return res.status(200).json(user);
} catch (e) {
return res.status(401).json({ message: e.message });
}
const user = await this.userService.loginUser(username, password);
req.session.user = user;
return res.status(200).json(user);
}
}

View File

@ -1,6 +1,7 @@
import joi from 'joi';
import { Response } from 'express';
import { Logger } from '../logger';
import BaseError from '../error/base-error';
export const customJoi = joi.extend((j) => ({
type: 'isUrlFriendly',
@ -29,6 +30,11 @@ export const handleErrors: (
// @ts-ignore
// eslint-disable-next-line no-param-reassign
error.isJoi = true;
if (error instanceof BaseError) {
return res.status(error.statusCode).json(error).end();
}
switch (error.name) {
case 'ValidationError':
return res.status(400).json(error).end();

View File

@ -24,6 +24,8 @@ import { RoleName } from '../types/model';
import SettingService from './setting-service';
import { SimpleAuthSettings } from '../server-impl';
import { simpleAuthKey } from '../types/settings/simple-auth-settings';
import DisabledError from '../error/disabled-error';
import PasswordMismatch from '../error/password-mismatch';
const systemUser = new User({ id: -1, username: 'system' });
@ -273,8 +275,8 @@ class UserService {
simpleAuthKey,
);
if (settings && settings.disabled) {
throw new Error(
if (settings?.disabled) {
throw new DisabledError(
'Logging in with username/password has been disabled.',
);
}
@ -290,7 +292,7 @@ class UserService {
await this.store.successfullyLogin(user);
return user;
}
throw new Error('Wrong password, try again.');
throw new PasswordMismatch();
}
/**