UNPKG

3.98 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, rawResponse?: Response): 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 gError.headers = rawResponse?.headers;
51 return gError;
52 }
53
54 /**
55 * @private
56 * @static
57 * @async
58 * Populates the GraphError instance from the Error returned by graph service
59 * @param {GraphAPIErrorResponse} graphError - The error possibly returned by graph service or some native error
60 * @param {number} statusCode - The status code of the response
61 * @returns A promise that resolves to GraphError instance
62 *
63 * Example error for https://graph.microsoft.com/v1.0/me/events?$top=3&$search=foo
64 * {
65 * "error": {
66 * "code": "SearchEvents",
67 * "message": "The parameter $search is not currently supported on the Events resource.",
68 * "innerError": {
69 * "request-id": "b31c83fd-944c-4663-aa50-5d9ceb367e19",
70 * "date": "2016-11-17T18:37:45"
71 * }
72 * }
73 * }
74 */
75 private static constructErrorFromResponse(graphError: GraphAPIErrorResponse, statusCode: number, rawResponse?: Response): GraphError {
76 const error = graphError.error;
77 const gError = new GraphError(statusCode, error.message);
78 gError.code = error.code;
79 if (error.innerError !== undefined) {
80 gError.requestId = error.innerError["request-id"];
81 gError.date = new Date(error.innerError.date);
82 }
83
84 gError.body = JSON.stringify(error);
85 gError.headers = rawResponse?.headers;
86
87 return gError;
88 }
89
90 /**
91 * @public
92 * @static
93 * @async
94 * To get the GraphError object
95 * Reference - https://docs.microsoft.com/en-us/graph/errors
96 * @param {any} [error = null] - The error returned by graph service or some native error
97 * @param {number} [statusCode = -1] - The status code of the response
98 * @param {GraphRequestCallback} [callback] - The graph request callback function
99 * @returns A promise that resolves to GraphError instance
100 */
101 public static async getError(error: any = null, statusCode = -1, callback?: GraphRequestCallback, rawResponse?: Response): Promise<GraphError> {
102 let gError: GraphError;
103 if (error && error.error) {
104 gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode, rawResponse);
105 } else if (error instanceof Error) {
106 gError = GraphErrorHandler.constructError(error, statusCode, rawResponse);
107 } else {
108 gError = new GraphError(statusCode);
109 gError.body = error; // if a custom error is passed which is not instance of Error object or a graph API response
110 }
111 if (typeof callback === "function") {
112 callback(gError, null);
113 } else {
114 return gError;
115 }
116 }
117}