All files / src StormError.ts

88.24% Statements 15/17
50% Branches 1/2
100% Functions 5/5
88.24% Lines 15/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95                              1x   1x                         1x       23x   23x   23x 23x 23x                             49x               56x       2x                 40x   40x         40x     40x                
// Copyright (C) 2017  Norman Breau
 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
import {getInstance} from './instance';
import {Application} from './Application';
import {StatusCode} from './StatusCode';
 
export interface IAdditionalErrorDetails {
    [key: string]: any;
}
 
export interface IErrorResponse {
    name: string;
    message: string;
    code: number;
    details: IAdditionalErrorDetails;
}
 
export abstract class StormError extends Error {
    private details: any;
 
    public constructor(details?: any) {
        super();
 
        this.details = details;
 
        const instance: Application = getInstance();
        instance.getLogger().error(`${this.getMessage()}... See details below:`);
        instance.getLogger().info(this.getPrivateDetails());
    }
 
    public abstract getMessage(): string;
    public abstract getCode(): number;
 
    // public getDetails(): any {
    //     getInstance().getLogger().deprecate('getPrivateDetails()');
    //     return this.getPrivateDetails();
    // }
 
    /**
     * Sends details to the client.
     */
    public getPublicDetails(): IAdditionalErrorDetails {
        return {};
    }
 
    /**
     * Private details are only logged to the server log.
     * They are kept secret from the client.
     */
    public getPrivateDetails(): any {
        return this.details;
    }
 
    public getHTTPCode(): StatusCode {
        return StatusCode.INTERNAL_ERROR;
    }
 
    // public getAdditionalDetails(): IAdditionalErrorDetails {
    //     getInstance().getLogger().deprecate('getPublicDetails()');
    //     return this.getPublicDetails();
    // }
 
    public getErrorResponse(): IErrorResponse {
        let details: IAdditionalErrorDetails = null;
 
        Iif ((<any>this)['getAdditionalDetails']) {
            getInstance().getLogger().deprecate('getPublicDetails', `${this.constructor.name}.getAdditionalDetails()`);
            details = (<any>this)['getAdditionalDetails']();
        }
        else {
            details = this.getPublicDetails();
        }
 
        return {
            name: this.constructor.name,
            message : this.getMessage(),
            code : this.getCode(),
            details: details
        };
    }
}