All files badRequestError.ts

55.56% Statements 5/9
33.33% Branches 2/6
33.33% Functions 1/3
55.56% Lines 5/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 391x                                       1x                 8x     8x     8x      
import { BaseError } from './baseError';
 
// Copied from https://github.com/lodash/lodash/blob/4.0.1-npm-packages/lodash.head/index.js
function _head(array) {
    return (array && array.length) ? array[0] : undefined;
}
 
export interface ValidationErrorError {
    field: String;
    location: String;
    messages: Array<string>;
    types: Array<string>;
}
 
export interface ValidationError {
    status: String;
    statusText: String;
    errors: Array<ValidationErrorError>;
}
 
export class BadRequestError extends BaseError {
    public static createFromValidationError(validationError: ValidationError) {
        const primaryError = <ValidationErrorError>_head(validationError.errors);
        const errorMessage = _head(primaryError.messages);
        return new BadRequestError(errorMessage);
    }
 
    constructor(message: string = 'Bad Request', status: number = 400) {
        // Calling parent constructor of base Error class.
        super(message, status, 'bad_request_error');
 
        // Capturing stack trace, excluding constructor call from it.
        Error.captureStackTrace(this, this.constructor);
 
        // Saving class name in the property of our custom error as a shortcut.
        this.name = this.constructor.name;
    }
}