5
5
* This source code is licensed under the license found in the
6
6
* LICENSE file in the root directory of this source tree.
7
7
*/
8
+ // request-promise error types
9
+ const REQUEST_ERROR = 'RequestError' ;
10
+ const STATUS_CODE_ERROR = 'StatusCodeError' ;
11
+
8
12
function FacebookError ( error ) {
9
13
this . name = 'FacebookError' ;
10
14
this . message = error . message ;
@@ -24,17 +28,63 @@ export class FacebookRequestError extends FacebookError {
24
28
* @param {Object } data
25
29
*/
26
30
constructor ( response , method , url , data ) {
27
- let error = response . body . error ;
28
- let message = error . error_user_msg
29
- ? `${ error . error_user_title } : ${ error . error_user_msg } `
30
- : error . message ;
31
- super ( message ) ;
31
+ const errorResponse = constructErrorResponse ( response ) ;
32
+
33
+ super ( errorResponse ) ;
32
34
this . name = 'FacebookRequestError' ;
33
- this . message = message ;
34
- this . status = response . status ;
35
- this . response = response . body ;
35
+ this . message = errorResponse . message ;
36
+ this . status = errorResponse . status ;
37
+ this . response = errorResponse . body ;
36
38
this . method = method ;
37
39
this . url = url ;
38
- if ( data ) this . data = data ;
40
+ if ( data ) {
41
+ this . data = data ;
42
+ }
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Error response has several structures depended on called APIs or errors.
48
+ * This method contructs and formats the response into the same structure for
49
+ * creating a FacebookRequestError object.
50
+ */
51
+ function constructErrorResponse ( response : Object ) {
52
+ let body ;
53
+ let message ;
54
+ let status ;
55
+
56
+ // Batch request error contains code and body fields
57
+ const isBatchResponse = response . code && response . body ;
58
+
59
+ if ( isBatchResponse ) {
60
+ // Handle batch response
61
+ body = typeof response . body === 'string'
62
+ ? JSON . parse ( response . body )
63
+ : response . body ;
64
+ status = response . code ;
65
+ message = body . error . message ;
66
+ } else {
67
+ // Handle single response
68
+ if ( response . name === STATUS_CODE_ERROR ) {
69
+ // Handle when we can get response error code
70
+ body = response . error ? response . error : response ;
71
+ body = typeof body === 'string'
72
+ ? JSON . parse ( body )
73
+ : body ;
74
+ // Construct an error message from subfields in body.error
75
+ message = body . error . error_user_msg
76
+ ? `${ body . error . error_user_title } : ${ body . error . error_user_msg } `
77
+ : body . error . message ;
78
+ status = response . statusCode ;
79
+ } else if ( response . name === REQUEST_ERROR ) {
80
+ // Handle network errors e.g. timeout, destination unreachable
81
+ body = { error : response . error } ;
82
+ // An error message is in the response already
83
+ message = response . message ;
84
+ // Network errors have no status code
85
+ status = null ;
86
+ }
39
87
}
88
+
89
+ return { body, message, status} ;
40
90
}
0 commit comments