-
Notifications
You must be signed in to change notification settings - Fork 223
/
exceptions.js
98 lines (90 loc) · 2.82 KB
/
exceptions.js
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
96
97
98
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
// request-promise error types
const REQUEST_ERROR = 'RequestError';
const STATUS_CODE_ERROR = 'StatusCodeError';
function FacebookError(error) {
this.name = 'FacebookError';
this.message = error.message;
this.stack = new Error().stack;
}
FacebookError.prototype = Object.create(Error.prototype);
FacebookError.prototype.constructor = FacebookError;
/**
* Raised when an api request fails.
*/
export class FacebookRequestError extends FacebookError {
/**
* @param {[Object} response
* @param {String} method
* @param {String} url
* @param {Object} data
*/
constructor(response, method, url, data) {
const errorResponse = constructErrorResponse(response);
super(errorResponse);
this.name = 'FacebookRequestError';
this.message = errorResponse.message;
this.status = errorResponse.status;
this.response = errorResponse.body;
this.headers = errorResponse.headers;
this.method = method;
this.url = url;
if (data) {
this.data = data;
}
}
}
/**
* Error response has several structures depended on called APIs or errors.
* This method contructs and formats the response into the same structure for
* creating a FacebookRequestError object.
*/
function constructErrorResponse(response: Object) {
let body;
let message;
let status;
let headers;
// Batch request error contains code and body fields
const isBatchResponse = response.code && response.body;
if (isBatchResponse) {
// Handle batch response
body =
typeof response.body === 'string'
? JSON.parse(response.body)
: response.body;
status = response.code;
message = body.error.message;
headers = response.headers;
} else {
// Handle single response
if (response.name === STATUS_CODE_ERROR) {
// Handle when we can get response error code
body = response.error ? response.error : response;
body = typeof body === 'string' ? JSON.parse(body) : body;
// Construct an error message from subfields in body.error
message = body.error.error_user_msg
? `${body.error.error_user_title}: ${body.error.error_user_msg}`
: body.error.message;
status = response.statusCode;
if (response.response) {
headers = response.response.headers;
}
} else if (response.name === REQUEST_ERROR) {
// Handle network errors e.g. timeout, destination unreachable
body = {error: response.error};
// An error message is in the response already
message = response.message;
// Network errors have no status code
status = null;
}
}
return {body, message, status, headers};
}