errors.ts 1.39 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
Deshui Yu's avatar
Deshui Yu committed
3
4
5
6
7
8
9
10
11
12

'use strict';

export namespace NNIErrorNames {
    export const NOT_FOUND: string = 'NOT_FOUND';
    export const INVALID_JOB_DETAIL: string = 'NO_VALID_JOB_DETAIL_FOUND';
    export const RESOURCE_NOT_AVAILABLE: string = 'RESOURCE_NOT_AVAILABLE';
}

export class NNIError extends Error {
chicm-ms's avatar
chicm-ms committed
13
    public cause!: Error | undefined;
14
    constructor (name: string, message: string, err?: Error) {
Deshui Yu's avatar
Deshui Yu committed
15
16
        super(message);
        this.name = name;
17
18
19
        if (err !== undefined) {
            this.stack = err.stack;
        }
chicm-ms's avatar
chicm-ms committed
20
        this.cause = err;
Deshui Yu's avatar
Deshui Yu committed
21
    }
chicm-ms's avatar
chicm-ms committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

    public static FromError(err: NNIError | Error | string, messagePrefix?: string): NNIError {
        const msgPrefix: string = messagePrefix === undefined ? '' : messagePrefix;
        if (err instanceof NNIError) {
            if (err.message !== undefined) {
                err.message = msgPrefix + err.message;
            }

            return err;
        } else if (typeof(err) === 'string') {
            return new NNIError('', msgPrefix + err);
        } else if (err instanceof Error) {
            return new NNIError('', msgPrefix + err.message, err);
        } else {
            throw new Error(`Wrong instance type: ${typeof(err)}`);
        }
    }
Deshui Yu's avatar
Deshui Yu committed
39
40
41
42
43
44
45
}

export class MethodNotImplementedError extends Error {
    constructor() {
        super('Method not implemented.');
    }
}