Skip to content

Commit 604a1f8

Browse files
committed
Release of v2.11.3 that includes the following bug fixes:
- Handle errors from batch API - Handle network errors, e.g. timeout, destination unreachable
1 parent fc28683 commit 604a1f8

File tree

7 files changed

+72
-25
lines changed

7 files changed

+72
-25
lines changed

CONTRIBUTING.md

-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
We want to make contributing to this project as easy and transparent as
33
possible.
44

5-
## Code of Conduct
6-
7-
The code of conduct is described in [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md)
8-
95
## Our Development Process
106
The Facebook Ads API SDK for NodeJS is currently developed in Facebook's internal
117
repositories and then exported out to GitHub by a Facebook team member. We invite

bower.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "facebook-ads-sdk",
3-
"version": "2.11.1",
3+
"version": "2.11.3",
44
"description": "SDK for the Facebook Ads API in Javascript and Node.js",
55
"authors": [
6-
"Zain Aziz<[email protected]>",
6+
"Zain Aziz <[email protected]>",
77
"Neil Chen <[email protected]>",
88
"Cloud Xu <[email protected]>",
99
"Supasate Choochaisri <[email protected]>"

gulpfile.js

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ function roll (format, entry, name, entry_dir, dest_dir) {
3131
format: format,
3232
exports: 'named',
3333
moduleName: 'fb',
34+
external: ['fs', 'path'],
35+
globals: {
36+
'fs': 'fs',
37+
'path': 'path',
38+
},
3439
plugins: [
3540
babel(babelrc.default({
3641
config: {

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "facebook-nodejs-ads-sdk",
3-
"version": "2.11.1",
3+
"version": "2.11.3",
44
"description": "SDK for the Facebook Ads API in Javascript and Node.js",
55
"author": "Facebook",
66
"maintainers": [
77
"Zain Aziz <[email protected]>, Neil Chen <[email protected]>, Cloud Xu <[email protected]>, Supasate Choochaisri <[email protected]>, lucascosta <[email protected]>"
88
],
9-
"homepage": "https://github.com/facebook/facebook-js-ads-sdk",
9+
"homepage": "https://github.com/facebook/facebook-nodejs-ads-sdk",
1010
"bugs": {
11-
"url": "https://github.com/facebook/facebook-js-ads-sdk/issues"
11+
"url": "https://github.com/facebook/facebook-nodejs-ads-sdk/issues"
1212
},
1313
"keywords": [
1414
"facebook",
@@ -27,7 +27,7 @@
2727
],
2828
"repository": {
2929
"type": "git",
30-
"url": "git://github.com/facebook/facebook-js-ads-sdk.git"
30+
"url": "git://github.com/facebook/facebook-nodejs-ads-sdk.git"
3131
},
3232
"devDependencies": {
3333
"babel-cli": "^6.24.1",

src/api-response.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import HTTP_STATUS from './http-status';
1212
import {FacebookRequestError} from './exceptions';
1313
/**
14-
* Encapsulates an http response from Facebook's Graph API.
15-
*/
14+
* Encapsulates an http response from Facebook's Graph API.
15+
*/
1616
class APIResponse {
1717
_body: string;
1818
_httpStatus: string;

src/exceptions.js

+59-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* This source code is licensed under the license found in the
66
* LICENSE file in the root directory of this source tree.
77
*/
8+
// request-promise error types
9+
const REQUEST_ERROR = 'RequestError';
10+
const STATUS_CODE_ERROR = 'StatusCodeError';
11+
812
function FacebookError (error) {
913
this.name = 'FacebookError';
1014
this.message = error.message;
@@ -24,17 +28,63 @@ export class FacebookRequestError extends FacebookError {
2428
* @param {Object} data
2529
*/
2630
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);
3234
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;
3638
this.method = method;
3739
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+
}
3987
}
88+
89+
return {body, message, status};
4090
}

src/http.js

-4
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ export default class Http {
119119
}
120120

121121
return requestPromise(options).catch((response: Object) => {
122-
response = {
123-
body: response.error ? response.error : response,
124-
status: response.statusCode
125-
};
126122
throw response;
127123
});
128124
}

0 commit comments

Comments
 (0)