UNPKG

3.8 kBPlain TextView Raw
1/**
2 * -------------------------------------------------------------------------------------------
3 * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
4 * See License in the project root for license information.
5 * -------------------------------------------------------------------------------------------
6 */
7
8/**
9 * @module GraphErrorHandler
10 */
11
12import { GraphError } from "./GraphError";
13import { GraphRequestCallback } from "./IGraphRequestCallback";
14
15/**
16 * @interface
17 * Signature for the json represent of the error response from the Graph API
18 * https://docs.microsoft.com/en-us/graph/errors
19 * @property {[key: string] : string | number} - The Key value pair
20 */
21interface GraphAPIErrorResponse {
22 error: {
23 code: string;
24 message: string;
25 innerError: any;
26 };
27}
28
29/**
30 * @class
31 * Class for GraphErrorHandler
32 */
33
34export class GraphErrorHandler {
35 /**
36 * @private
37 * @static
38 * Populates the GraphError instance with Error instance values
39 * @param {Error} error - The error returned by graph service or some native error
40 * @param {number} [statusCode] - The status code of the response
41 * @returns The GraphError instance
42 */
43 private static constructError(error: Error, statusCode?: number): GraphError {
44 const gError = new GraphError(statusCode, "", error);
45 if (error.name !== undefined) {
46 gError.code = error.name;
47 }
48 gError.body = error.toString();
49 gError.date = new Date();
50 return gError;
51 }
52
53 /**
54 * @private
55 * @static
56 * @async
57 * Populates the GraphError instance from the Error returned by graph service
58 * @param {GraphAPIErrorResponse} graphError - The error possibly returned by graph service or some native error
59 * @param {number} statusCode - The status code of the response
60 * @returns A promise that resolves to GraphError instance
61 *
62 * Example error for https://graph.microsoft.com/v1.0/me/events?$top=3&$search=foo
63 * {
64 * "error": {
65 * "code": "SearchEvents",
66 * "message": "The parameter $search is not currently supported on the Events resource.",
67 * "innerError": {
68 * "request-id": "b31c83fd-944c-4663-aa50-5d9ceb367e19",
69 * "date": "2016-11-17T18:37:45"
70 * }
71 * }
72 * }
73 */
74 private static constructErrorFromResponse(graphError: GraphAPIErrorResponse, statusCode: number): GraphError {
75 const error = graphError.error;
76 const gError = new GraphError(statusCode, error.message);
77 gError.code = error.code;
78 if (error.innerError !== undefined) {
79 gError.requestId = error.innerError["request-id"];
80 gError.date = new Date(error.innerError.date);
81 }
82
83 gError.body = JSON.stringify(error);
84
85 return gError;
86 }
87
88 /**
89 * @public
90 * @static
91 * @async
92 * To get the GraphError object
93 * Reference - https://docs.microsoft.com/en-us/graph/errors
94 * @param {any} [error = null] - The error returned by graph service or some native error
95 * @param {number} [statusCode = -1] - The status code of the response
96 * @param {GraphRequestCallback} [callback] - The graph request callback function
97 * @returns A promise that resolves to GraphError instance
98 */
99 public static async getError(error: any = null, statusCode = -1, callback?: GraphRequestCallback): Promise<GraphError> {
100 let gError: GraphError;
101 if (error && error.error) {
102 gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode);
103 } else if (error instanceof Error) {
104 gError = GraphErrorHandler.constructError(error, statusCode);
105 } else {
106 gError = new GraphError(statusCode);
107 gError.body = error; // if a custom error is passed which is not instance of Error object or a graph API response
108 }
109 if (typeof callback === "function") {
110 callback(gError, null);
111 } else {
112 return gError;
113 }
114 }
115}