Skip to content

Commit 2eabdd3

Browse files
committed
Improving error handling
1 parent a2500ca commit 2eabdd3

File tree

13 files changed

+134
-34
lines changed

13 files changed

+134
-34
lines changed

.node-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12
1+
14

DEVNOTES.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,14 @@ I should make sure I return the same response if possible.
2020
<notifyResult success="false" msg="The following parameters were missing from the request body: notifyProcedure, port, path, protocol." />
2121
```
2222

23+
I think I check for all of these except notifyProcedure.
24+
25+
## Update README docs
26+
27+
Fully spec out XML-RPC endpoints with code samples.
28+
29+
## Build freestanding rssCloud testing tool
30+
31+
Hopefully this can be based on the test suite I'm creating now, but it will be a hosted app that has a dashboard where you can put in the details for an rssCloud server. It mocks the RSS feed and aggregator endpoints so it can direct the real rssCloud server at resources it controls. This way it can run through a series of tests with red/greed lights to see if each test passes.
32+
33+
Split out rssCloud.root functionality vs my extended functionality.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# rssCloud Server
1+
# rssCloud Server v2
22

33
[![MIT License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/rsscloud/rsscloud-server/blob/2.x/LICENSE.md)
44
[![rssCloud Server 2.x](https://circleci.com/gh/rsscloud/rsscloud-server/tree/2.x.svg?style=shield)](https://circleci.com/gh/rsscloud/rsscloud-server/tree/2.x)

controllers/ping.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@
3535
}
3636

3737
router.post('/', urlencodedParser, function (req, res) {
38-
const params = parsePingParams(req);
39-
ping(params.url)
40-
.then(result => processResponse(req, res, result))
41-
.catch(err => handleError(req, res, err));
38+
try {
39+
const params = parsePingParams.rest(req);
40+
ping(params.url)
41+
.then(result => processResponse(req, res, result))
42+
.catch(err => handleError(req, res, err));
43+
} catch (err) {
44+
return handleError(req, res, err);
45+
}
4246
});
4347

4448
module.exports = router;

controllers/rpc2.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
express = require('express'),
66
parseRpcRequest = require('../services/parse-rpc-request'),
77
parseNotifyParams = require('../services/parse-notify-params'),
8+
parsePingParams = require('../services/parse-ping-params'),
89
pleaseNotify = require('../services/please-notify'),
910
ping = require('../services/ping'),
1011
router = new express.Router(),
@@ -25,35 +26,44 @@
2526
}
2627

2728
function handleError(req, res, err) {
28-
// console.error(err);
29+
console.error(err);
2930
processResponse(req, res, rpcReturnFault(4, err.message));
3031
}
3132

3233
router.post('/', textParser, function (req, res) {
34+
let params;
3335
parseRpcRequest(req)
3436
.then(request => {
3537
switch (request.methodName) {
3638
case 'rssCloud.hello':
37-
console.log(request.params[0]);
3839
processResponse(req, res, rpcReturnSuccess(true));
3940
break;
4041
case 'rssCloud.pleaseNotify':
41-
const params = parseNotifyParams.rpc(req, request.params);
42-
pleaseNotify(
43-
params.notifyProcedure,
44-
params.apiurl,
45-
params.protocol,
46-
params.urlList,
47-
params.diffDomain
48-
)
49-
.then(result => processResponse(req, res, rpcReturnSuccess(result.success)))
50-
.catch(err => handleError(req, res, err));
42+
try {
43+
params = parseNotifyParams.rpc(req, request.params);
44+
pleaseNotify(
45+
params.notifyProcedure,
46+
params.apiurl,
47+
params.protocol,
48+
params.urlList,
49+
params.diffDomain
50+
)
51+
.then(result => processResponse(req, res, rpcReturnSuccess(result.success)))
52+
.catch(err => handleError(req, res, err));
53+
} catch (err) {
54+
handleError(req, res, err);
55+
}
5156
break;
5257
case 'rssCloud.ping':
53-
// Dave's rssCloud server always returns true whether it succeeded or not
54-
ping(request.params[0])
55-
.then(result => processResponse(req, res, rpcReturnSuccess(result.success)))
56-
.catch(err => processResponse(req, res, rpcReturnSuccess(true)));
58+
try {
59+
params = parsePingParams.rpc(req, request.params);
60+
// Dave's rssCloud server always returns true whether it succeeded or not
61+
ping(params.url)
62+
.then(result => processResponse(req, res, rpcReturnSuccess(result.success)))
63+
.catch(err => processResponse(req, res, rpcReturnSuccess(true)));
64+
} catch (err) {
65+
handleError(req, res, err);
66+
}
5767
break;
5868
default:
5969
handleError(

services/app-messages.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
ping: {
1414
tooRecent: 'Can\'t accept the request because the minimum seconds between pings is %s and you pinged us %s seconds ago.',
1515
readResource: 'The ping was cancelled because there was an error reading the resource at URL %s.'
16+
},
17+
rpc: {
18+
notEnoughParams: 'Can\'t call "%s" because there aren\'t enough parameters.',
19+
tooManyParams: 'Can\'t call "%s" because there are too many parameters.'
1620
}
1721
},
1822
log: {

services/parse-notify-params.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
let params = {},
107107
parts = {};
108108

109+
if (5 > rpcParams.length) {
110+
throw new Error(sprintf(appMessages.error.rpc.notEnoughParams, 'pleaseNotify'));
111+
} else if (6 < rpcParams.length) {
112+
throw new Error(sprintf(appMessages.error.rpc.tooManyParams, 'pleaseNotify'));
113+
}
114+
109115
if (validProtocol(rpcParams[3])) {
110116
params.protocol = rpcParams[3];
111117
}

services/parse-ping-params.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const appMessages = require('./app-messages'),
55
sprintf = require('sprintf-js').sprintf;
66

7-
function parsePingParams(req) {
7+
function rest(req) {
88
var s = '',
99
params = {};
1010

@@ -20,5 +20,19 @@
2020
}
2121
}
2222

23-
module.exports = parsePingParams;
23+
function rpc(req, rpcParams) {
24+
let params = {};
25+
26+
if (1 > rpcParams.length) {
27+
throw new Error(sprintf(appMessages.error.rpc.notEnoughParams, 'ping'));
28+
} else if (1 < rpcParams.length) {
29+
throw new Error(sprintf(appMessages.error.rpc.tooManyParams, 'ping'));
30+
}
31+
32+
params.url = rpcParams[0];
33+
34+
return params;
35+
}
36+
37+
module.exports = { rest, rpc };
2438
}());

test/ping.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ const chai = require("chai"),
66
mock = require("./mock"),
77
mongodb = require("./mongodb"),
88
xmlrpc = require("davexmlrpc"),
9-
rpcReturnSuccess = require('../services/rpc-return-success');
9+
rpcReturnSuccess = require('../services/rpc-return-success'),
10+
rpcReturnFault = require('../services/rpc-return-fault');
1011

1112
chai.use(chaiHttp);
1213
chai.use(chaiXml);
1314

1415
function ping(pingProtocol, resourceUrl, returnFormat) {
1516
if ('XML-RPC' === pingProtocol) {
16-
const rpctext = xmlrpc.buildCall('rssCloud.ping', [resourceUrl], 'xml');
17+
let rpctext;
18+
if (null == resourceUrl) {
19+
rpctext = xmlrpc.buildCall('rssCloud.ping', [], 'xml');
20+
console.log(rpctext);
21+
} else {
22+
rpctext = xmlrpc.buildCall('rssCloud.ping', [resourceUrl], 'xml');
23+
}
1724

1825
return chai
1926
.request(SERVER_URL)
@@ -30,7 +37,11 @@ function ping(pingProtocol, resourceUrl, returnFormat) {
3037
req.set('accept', 'application/json');
3138
}
3239

33-
return req.send({ url: resourceUrl });
40+
if (null == resourceUrl) {
41+
return req.send({});
42+
} else {
43+
return req.send({ url: resourceUrl });
44+
}
3445
}
3546
}
3647

@@ -151,6 +162,46 @@ for (const pingProtocol of ['XML-RPC', 'REST']) {
151162
}
152163
});
153164

165+
it('should reject a ping with a missing url', async () => {
166+
const feedPath = '/rss.xml',
167+
pingPath = '/feedupdated',
168+
resourceUrl = null;
169+
170+
let apiurl = ('http-post' === protocol ? mock.serverUrl : mock.secureServerUrl) + pingPath,
171+
notifyProcedure = false;
172+
173+
if ('xml-rpc' === protocol) {
174+
apiurl = mock.serverUrl + '/RPC2';
175+
notifyProcedure = 'river.feedUpdated';
176+
}
177+
178+
mock.route('GET', feedPath, 404, 'Not Found');
179+
mock.route('POST', pingPath, 200, 'Thanks for the update! :-)');
180+
mock.rpc(notifyProcedure, rpcReturnSuccess(true));
181+
182+
let res = await ping(pingProtocol, resourceUrl, returnFormat);
183+
184+
expect(res).status(200);
185+
186+
if ('XML-RPC' === pingProtocol) {
187+
expect(res.text).xml.equal(rpcReturnFault(4, 'Can\'t call "ping" because there aren\'t enough parameters.'));
188+
} else {
189+
if ('JSON' === returnFormat) {
190+
expect(res.body).deep.equal({ success: false, msg: `The following parameters were missing from the request body: url.` });
191+
} else {
192+
expect(res.text).xml.equal(`<result success="false" msg="The following parameters were missing from the request body: url."/>`);
193+
}
194+
}
195+
196+
expect(mock.requests.GET).property(feedPath).lengthOf(0, `Should not GET ${feedPath}`);
197+
198+
if ('xml-rpc' === protocol) {
199+
expect(mock.requests.RPC2).property(notifyProcedure).lengthOf(0, `Should not XML-RPC call ${notifyProcedure}`);
200+
} else {
201+
expect(mock.requests.POST).property(pingPath).lengthOf(0, `Should not POST ${pingPath}`);
202+
}
203+
});
204+
154205
it('should accept a ping for unchanged resource', async () => {
155206
const feedPath = '/rss.xml',
156207
pingPath = '/feedupdated',

views/docs.handlebars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>rssCloud: Log</title>
5+
<title>rssCloud v2: Documentation</title>
66
<link href="/css/bootstrap.min.css" rel="stylesheet">
77
<link href="/css/font-awesome.min.css" rel="stylesheet">
88
</head>

0 commit comments

Comments
 (0)