File size: 1,097 Bytes
813eca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import _ from 'lodash';

import Body from './Body.ts';
import Exception from '../exceptions/Exception.ts';
import APIException from '../exceptions/APIException.ts';
import EX from '../consts/exceptions.ts';
import HTTP_STATUS_CODES from '../http-status-codes.ts';

export default class FailureBody extends Body {
    
    constructor(error: APIException | Exception | Error, _data?: any) {
        let errcode, errmsg, data = _data, httpStatusCode = HTTP_STATUS_CODES.OK;;
        if(_.isString(error))
            error = new Exception(EX.SYSTEM_ERROR, error);
        else if(error instanceof APIException || error instanceof Exception)
            ({ errcode, errmsg, data, httpStatusCode } = error);
        else if(_.isError(error))
        ({ errcode, errmsg, data, httpStatusCode } = new Exception(EX.SYSTEM_ERROR, error.message));
        super({
            code: errcode || -1,
            message: errmsg || 'Internal error',
            data,
            statusCode: httpStatusCode
        });
    }

    static isInstance(value) {
        return value instanceof FailureBody;
    }

}