forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseHandler.ts
More file actions
68 lines (63 loc) · 2.52 KB
/
ResponseHandler.ts
File metadata and controls
68 lines (63 loc) · 2.52 KB
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
import {GraphRequestCallback, GraphError} from "./common"
export class ResponseHandler {
static init(res, err, resContents, callback:GraphRequestCallback):void {
if (res && res.ok) { // 2xx
callback(null, resContents, res)
} else { // not OK response
if (err == null && res != null)
if (resContents != null && resContents.error != null) // if error was passed to body
callback(ResponseHandler.buildGraphErrorFromResponseObject(resContents.error, res.status), null, res);
else
callback(ResponseHandler.defaultGraphError(res.status), null, res)
else // pass back error as first param
callback(ResponseHandler.ParseError(err), null, res)
}
}
/*
Example error for https://graph.microsoft.com/v1.0/me/events?$top=3&$search=foo
{
"error": {
"code": "SearchEvents",
"message": "The parameter $search is not currently supported on the Events resource.",
"innerError": {
"request-id": "b31c83fd-944c-4663-aa50-5d9ceb367e19",
"date": "2016-11-17T18:37:45"
}
}
}
*/
static ParseError(rawErr: Error):GraphError {
// if we couldn't find an error obj to parse, just return an object with a status code and date
if (!rawErr) {
return ResponseHandler.defaultGraphError(-1);
}
return ResponseHandler.buildGraphErrorFromErrorObject(rawErr);
}
static defaultGraphError(statusCode: number):GraphError {
return {
statusCode,
code: null,
message: null,
requestId: null,
date: new Date(),
body: null
}
}
static buildGraphErrorFromErrorObject(errObj: Error):GraphError {
const error: GraphError = ResponseHandler.defaultGraphError(-1);
error.body = errObj.toString();
error.message = errObj.message;
error.date = new Date();
return error;
}
static buildGraphErrorFromResponseObject(errObj: any, statusCode: number):GraphError {
return {
statusCode,
code: errObj.code,
message: errObj.message,
requestId: (errObj.innerError !== undefined) ? errObj.innerError["request-id"] : "",
date: (errObj.innerError !== undefined) ? new Date(errObj.innerError.date): new Date(),
body: errObj
};
}
}