Skip to content

Commit 276d5da

Browse files
committed
correct errors thrown everywhere
also update README with examples and update version to 0.4.13
1 parent e3b7044 commit 276d5da

20 files changed

+225
-182
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ apigeeEdge.connect(options)
3333
return org.developers.create(options)
3434
.then( (result) => console.log('ok. developer: ' + JSON.stringify(result)) )
3535
})
36-
.catch ( reason => {
37-
console.log('error: ' + reason.error.stack);
36+
.catch ( error => {
37+
console.log('error: ' + error);
3838
});
3939
```
4040

@@ -176,10 +176,10 @@ edgeOrg.proxies.export({name:'proxyname'})
176176
fs.writeFileSync(path.join('/Users/foo/export', result.filename), result.buffer);
177177
console.log('ok');
178178
})
179-
.catch( reason => { console.log(reason.error);});
179+
.catch( error => console.log(util.format(error)) );
180180
```
181181

182-
In the case of an error, the catch() will get the reason. There will be 2 members to the reason object: error, and result. The result is the payload send back, if any.
182+
In the case of an error, the catch() will get the Error object. There will be an additional member on the reason object: result. The result is the payload send back, if any.
183183

184184
using callbacks:
185185
```js
@@ -234,8 +234,8 @@ apigeeEdge.connect(options)
234234
org.proxies.import({name:opt.options.name, source:'/tmp/path/dir'})
235235
.then ( result =>
236236
console.log('import ok. %s name: %s r%d', term, result.name, result.revision) ) )
237-
.catch ( reason => {
238-
console.log('error: ' + reason.error.stack);
237+
.catch ( error => {
238+
console.log(util.format(error));
239239
});
240240
```
241241

@@ -276,7 +276,7 @@ var options = {
276276

277277
org.proxies.deploy(options)
278278
.then( result => console.log('deployment succeeded.') )
279-
.catch reason => console.log('deployment failed.') );
279+
.catch( error => console.log('deployment failed. ' + error) );
280280
```
281281
282282
@@ -313,11 +313,11 @@ apigeeEdge.connect(options)
313313
items
314314
.reduce(reducer, Promise.resolve([]))
315315
.then( (arrayOfResults) => common.logWrite('all done...\n' + JSON.stringify(arrayOfResults)) )
316-
.catch( (e) => console.error('error: ' + e.stack) );
316+
.catch( e => console.error('error: ' + e) );
317317

318318
});
319319
})
320-
.catch( reason => console.error('error: ' + reason.error.stack) );
320+
.catch( e => console.error(e) );
321321
```
322322
323323
@@ -394,7 +394,7 @@ apigeeEdge.connect(options)
394394
.then( () => org.maskconfigs.add({ namespaces : { prefix:'apigee', value:'urn://apigee' } })
395395
.then( (result) => console.log(JSON.stringify(result)) )
396396
})
397-
.catch ( reason => console.log('error: ' + reason.error.stack) );
397+
.catch ( e => console.log(e) );
398398
```
399399
400400
### Create a Target Server
@@ -417,7 +417,7 @@ apigeeEdge.connect(options)
417417
}
418418
});
419419
})
420-
.catch ( reason => console.log('error: ' + reason.error.stack) );
420+
.catch ( e => console.log(e) );
421421

422422
```
423423
@@ -435,8 +435,8 @@ apigeeEdge.connect(connectOptions)
435435
.then( result => {
436436
...
437437
})
438-
.catch( reason => {
439-
console.log('failed to create: ' + reason.error);
438+
.catch( e => {
439+
console.log('failed to create: ' + e);
440440
});
441441
});
442442
```
@@ -454,8 +454,8 @@ apigeeEdge.connect(connectOptions)
454454
.then ( result => {
455455
console.log('attrs: ' + JSON.stringify(result.attributes));
456456
})
457-
.catch( reason => {
458-
console.log('failed to create: ' + reason.error);
457+
.catch( e => {
458+
console.log('failed to update: ' + e);
459459
});
460460
});
461461
```

examples/deployTool.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// See the License for the specific language governing permissions and
2121
// limitations under the License.
2222
//
23-
// last saved: <2019-February-11 12:49:12>
23+
// last saved: <2019-September-05 15:22:42>
2424

2525
const edgejs = require('apigee-edge-js'),
2626
common = edgejs.utility,
@@ -105,4 +105,4 @@ apigeeEdge.connect(common.optToOptions(opt))
105105
.catch( (e) => console.error('error: ' + e.stack) );
106106
}
107107
})
108-
.catch( (e) => { console.error('error: ' + e.error);} );
108+
.catch( (e) => console.error('error: ' + e) );

examples/getToken.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// See the License for the specific language governing permissions and
1919
// limitations under the License.
2020
//
21-
// last saved: <2019-July-08 16:07:40>
21+
// last saved: <2019-September-05 15:24:02>
2222

2323
const edgejs = require('apigee-edge-js'),
2424
common = edgejs.utility,
@@ -56,4 +56,4 @@ apigeeEdge.connect(common.optToOptions(opt))
5656
}
5757
});
5858
})
59-
.catch( e => { console.error('error: ' + e);} );
59+
.catch( e => console.error('error: ' + e) );

examples/importAndDeploy.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
// See the License for the specific language governing permissions and
1919
// limitations under the License.
2020
//
21-
// last saved: <2019-June-04 10:50:42>
21+
// last saved: <2019-August-27 14:16:40>
2222

2323
const edgejs = require('apigee-edge-js'),
2424
common = edgejs.utility,
25+
util = require('util'),
2526
apigeeEdge = edgejs.edge,
2627
sprintf = require('sprintf-js').sprintf,
2728
Getopt = require('node-getopt'),
28-
version = '20190211-1248',
29+
version = '20190827-1405',
2930
defaults = { basepath : '/' },
3031
getopt = new Getopt(common.commonOptions.concat([
3132
['d' , 'source=ARG', 'source directory for the proxy files. Should be parent of dir "apiproxy" or "sharedflowbundle"'],
@@ -41,6 +42,9 @@ console.log(
4142
'Apigee Edge Proxy/Sharedflow Import + Deploy tool, version: ' + version + '\n' +
4243
'Node.js ' + process.version + '\n');
4344

45+
process.on('unhandledRejection',
46+
r => console.log('\n*** unhandled promise rejection: ' + util.format(r)));
47+
4448
common.logWrite('start');
4549

4650
// process.argv array starts with 'node' and 'scriptname.js'
@@ -60,8 +64,9 @@ if (opt.options.basepath && opt.options.sharedflow) {
6064

6165
common.verifyCommonRequiredParameters(opt.options, getopt);
6266

63-
apigeeEdge.connect(common.optToOptions(opt))
64-
.then( (org) => {
67+
apigeeEdge
68+
.connect(common.optToOptions(opt))
69+
.then( org => {
6570
common.logWrite('connected');
6671

6772
const collection = (opt.options.sharedflow) ? org.sharedflows : org.proxies;
@@ -89,19 +94,22 @@ apigeeEdge.connect(common.optToOptions(opt))
8994
const reducer = (promise, env) =>
9095
promise .then( () =>
9196
collection
92-
.deploy(Object.assign(options, { environment:env }))
93-
.then( (result) => common.logWrite('deployment ' + ((result.error) ? 'failed: ' + JSON.stringify(result) : 'ok.') ))
97+
.deploy(Object.assign(options, { environment:env }))
98+
.then( (result) => common.logWrite('deployment ' + ((result.error) ? 'failed: ' + JSON.stringify(result) : 'ok.') ))
9499
);
95100

96101
return envs.split(',')
97102
.reduce(reducer, Promise.resolve())
98103
.then( () => common.logWrite('all done...') );
99-
//.catch( (e) => console.error('error: ' + e.stack) );
104+
//.catch( (e) => console.error('error: ' + e.stack) );
100105
}
101106
else {
102107
common.logWrite('not deploying...');
103108
common.logWrite('finish');
104109
}
105110
});
111+
106112
})
107-
.catch( (e) => { console.error('error: ' + e);} );
113+
.catch( e => {
114+
console.log('while connecting, error: ' + e);
115+
} );

examples/resourceTool.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// See the License for the specific language governing permissions and
2020
// limitations under the License.
2121
//
22-
// last saved: <2019-March-06 08:51:51>
22+
// last saved: <2019-September-05 15:23:39>
2323

2424
const edgejs = require('apigee-edge-js'),
2525
common = edgejs.utility,
@@ -122,4 +122,4 @@ apigeeEdge.connect(common.optToOptions(opt))
122122
});
123123
}
124124
})
125-
.catch( (e) => { console.error('error: ' + e.error);} );
125+
.catch( (e) => console.error('error: ' + e) );

lib/cache.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
var name = options.name || options.cache || options.cacheName;
3333
var env = options.env || options.environment;
3434
if (!env) {
35-
return cb({error:"missing environment name for cache"});
35+
return cb(new Error('missing environment name for cache'));
3636
}
3737
common.insureFreshToken(conn, function(requestOptions) {
3838
requestOptions.url = (options.name) ?
@@ -55,10 +55,10 @@
5555
var name = options.name || options.cache || options.cacheName;
5656
var env = options.env || options.environment;
5757
if (!env) {
58-
return cb({error:"missing environment name for cache"});
58+
return cb(new Error('missing environment name for cache'));
5959
}
6060
if (!name) {
61-
return cb({error:"missing name for cache"});
61+
return cb(new Error("missing name for cache"));
6262
}
6363
if (conn.verbosity>0) {
6464
utility.logWrite(sprintf('Create Cache %s', name));
@@ -98,10 +98,10 @@
9898
var name = options.name || options.cache || options.cacheName;
9999
var env = options.env || options.environment;
100100
if (!env) {
101-
return cb({error:"missing environment name for cache"});
101+
return cb(new Error("missing environment name for cache"));
102102
}
103103
if (!name) {
104-
return cb({error:"missing name for cache"});
104+
return cb(new Error("missing name for cache"));
105105
}
106106
if (conn.verbosity>0) {
107107
utility.logWrite(sprintf('Delete Cache %s', name));

lib/common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
}
5858

5959
function safeJsonParse(response, body) {
60+
if ( ! response || !response.headers) return body;
6061
let isJson = jsonRe.test(response.headers["content-type"]);
6162
// this simple approach does not work because the
6263
// Edge API sometimes claims application/json erroneously.

lib/connection.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
return obj;
3636
}
3737

38+
function shortString(s){
39+
if (s.length > 32)
40+
s = s.substring(0, 32) + '...';
41+
return s.replace(new RegExp('\n', 'g'), ' ');
42+
}
43+
3844
function invokeTokenEndpoint(conn, formparams, cb) {
3945
var requestOptions = {
4046
headers: {
@@ -50,14 +56,20 @@
5056
request.post(requestOptions, common.callback(conn, [200], function(e, result) {
5157
if (conn.verbosity>0) {
5258
if (e) {
53-
utility.logWrite('POST error: ' + JSON.stringify(e));
59+
utility.logWrite('POST error: ' + e.message);
60+
}
61+
else if (typeof result == 'string') {
62+
utility.logWrite('POST result (string): ' + shortString(result));
5463
}
5564
else {
5665
var masked = mask(result);
5766
utility.logWrite('POST result: ' + JSON.stringify(masked));
5867
}
5968
}
60-
if ( ! e && result) {
69+
if (e) {
70+
return cb(e, result);
71+
}
72+
if (result && typeof result == 'object' && result.access_token) {
6173
result.issued_at = (new Date()).valueOf();
6274
if (formparams.username) {
6375
conn.user = formparams.username;
@@ -66,8 +78,10 @@
6678
conn.requestHeaders.authorization = 'Bearer ' + result.access_token;
6779
delete conn.org.passcode;
6880
delete conn.org.password;
81+
return cb(null, result);
6982
}
70-
cb(e, result);
83+
84+
return cb(new Error("invalid token response"), result);
7185
}));
7286
}
7387

lib/developer.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616
//
17+
/* global process */
1718

1819
(function (){
1920
'use strict';
@@ -46,7 +47,7 @@
4647
var conn = this.conn;
4748
var email = options.developerEmail || options.email;
4849
if ( !email || !options.firstName || !options.lastName || !options.userName) {
49-
return cb({error: "missing required inputs, one of {email, firstName, lastName, userName}"});
50+
return cb(new Error("missing required inputs, one of {email, firstName, lastName, userName}"));
5051
}
5152
if (conn.verbosity>0) {
5253
utility.logWrite(sprintf('Create Developer %s', email));
@@ -77,7 +78,7 @@
7778
// Authorization: :edge-auth
7879
var conn = this.conn;
7980
if ( !options.developerEmail) {
80-
return cb({error: "missing developerEmail"});
81+
return cb(new Error("missing developerEmail"));
8182
}
8283
if (conn.verbosity>0) {
8384
utility.logWrite(sprintf('Delete Developer %s', options.developerEmail));
@@ -111,11 +112,11 @@
111112
// POST -H content-type:application/octet-stream
112113
// /v1/o/ORGNAME/developers/DEVELOPERID?action=ACTION
113114
if(options.action != 'revoke' && options.action != 'approve') {
114-
return cb({error:"missing or invalid action"});
115+
return cb(new Error("missing or invalid action"));
115116
}
116117
var discriminator = options.developer || options.developerId || options.developerEmail || options.email ;
117118
if( ! discriminator) {
118-
return cb({error:"missing developer ID or email"});
119+
return cb(new Error("missing developer ID or email"));
119120
}
120121

121122
let urlTail = sprintf('developers/%s', discriminator);

lib/developerApp.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
var name = options.appName || options.name || options.app;
4949
var email = options.developer || options.developerEmail || options.email ;
5050
if ( !email || !name || !options.apiProduct) {
51-
return cb({error: "missing required inputs, one of {developer, appName, apiProduct}"});
51+
return cb(new Error("missing required inputs, one of {developer, appName, apiProduct}"));
5252
}
5353
if (conn.verbosity>0) {
5454
utility.logWrite(sprintf('Create App %s for %s', name, email));
@@ -76,8 +76,9 @@
7676
requestOptions.body = JSON.stringify({
7777
attributes : appAttributes,
7878
apiProducts: [options.apiProduct],
79-
keyExpiresIn : keyExpiresIn,
80-
name: name
79+
keyExpiresIn,
80+
name,
81+
callbackUrl: options.callbackUrl
8182
});
8283
if (conn.verbosity>0) {
8384
utility.logWrite(sprintf('POST %s', requestOptions.url));

0 commit comments

Comments
 (0)