Skip to content

Commit a87dbf0

Browse files
committed
More improvements: Support for Request Assertions, changed some error messages
1 parent de4f552 commit a87dbf0

5 files changed

Lines changed: 87 additions & 17 deletions

File tree

examples/basic/server/services/BlockService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const block = async (params) => {
111111
);
112112

113113
const otherTransactions = [
114-
new TransactionIdentifier('transaction 1'),
114+
new Types.TransactionIdentifier('transaction 1'),
115115
];
116116

117117
return new Types.BlockResponse(

lib/asserter/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class RosettaAsserter {
7979
);
8080

8181
if (index == -1) {
82-
throw new AsserterError(`SupportedNetwork ${JSON.stringify(networkIdentifier)} is not supported by this asserter`);
82+
throw new AsserterError(`SupportedNetwork ${JSON.stringify(networkIdentifier)} is not supported`);
8383
}
8484
}
8585

@@ -150,7 +150,7 @@ class RosettaAsserter {
150150
this.NetworkIdentifier(constructionSubmitRequest.network_identifier);
151151
this.SupportedNetwork(constructionSubmitRequest.network_identifier);
152152

153-
if (!constructionSubmitRequest.signed_transaction) {
153+
if (!constructionSubmitRequest.signed_transaction) {xr
154154
throw new AsserterError('ConstructionSubmitRequest.signed_transaction is empty');
155155
}
156156
}

lib/controllers/CallHandler.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright (c) 2020 DigiByte Foundation NZ Limited
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
* this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to
7+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8+
* the Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
/*
23+
CallHandler calls registered service handlers (if existing).
24+
Must be called with .bind(expressApp)
25+
*/
26+
27+
const CallAsserter = async function (asserter, className, params) {
28+
if (!asserter || !className || !params) return;
29+
30+
const validationFunc = asserter[className];
31+
if (!validationFunc) {
32+
console.log(`No validation func ${className} found`);
33+
return;
34+
}
35+
36+
return validationFunc.bind(asserter)(params);
37+
}
38+
39+
const CallHandler = async function (route, args) {
40+
const app = this;
41+
const routeHandlers = app.routeHandlers;
42+
43+
const data = routeHandlers[route];
44+
if (!data || !data.handler) {
45+
throw new Error(`Service for ${route} not implemented`);
46+
}
47+
48+
// Each route can have a specific asserter.
49+
// If a specific asserter was not set, the global asserter will
50+
// be used to validate requests, if set.
51+
const asserter = data.asserter || app.asserter;
52+
53+
// Retrieve the modelName that was set by the Controller (collectRequestParams).
54+
// Also retrieve the request POST args using the modelName.
55+
const modelName = args.params.class;
56+
const requestParamsKey = args.params.requestParamsKey;
57+
const requestParams = args.params[requestParamsKey];
58+
59+
// Try to call the asserter.
60+
await CallAsserter(asserter, modelName, requestParams);
61+
62+
return await data.handler(args);
63+
};
64+
65+
module.exports = CallHandler;

lib/controllers/Controller.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ class Controller {
4848
}
4949

5050
static sendError(response, error) {
51-
response.status(error.code || 500);
52-
if (error.error instanceof Object) {
53-
response.json(error.error);
54-
} else {
55-
response.end(error.error || error.message);
56-
}
51+
const errResponse = {
52+
code: error.code || 500,
53+
message: error.error || error.message,
54+
retriable: error.retriable || false,
55+
details: error.details,
56+
};
57+
58+
response.status(errResponse.code);
59+
response.json(errResponse);
5760
}
5861

5962
/**
@@ -138,8 +141,10 @@ class Controller {
138141
const schema = request.openapi.schema.requestBody.content['application/json'].schema;
139142

140143
if (schema.$ref) {
141-
let modelName = this.extractModelName(schema);
144+
let modelName = Controller.extractModelName(schema);
142145
requestParams[modelName] = request.body;
146+
requestParams['class'] = Controller.extractModelName(schema, false);
147+
requestParams['requestParamsKey'] = modelName;
143148

144149
} else {
145150
requestParams.body = request.body;

test/asserter.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,7 +2116,7 @@ describe('Asserter Tests', function () {
21162116
} catch (e) {
21172117
// console.error(e);
21182118
expect(e.name).to.equal('AsserterError');
2119-
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported by this asserter');
2119+
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported');
21202120
thrown = true;
21212121
}
21222122

@@ -2275,7 +2275,7 @@ describe('Asserter Tests', function () {
22752275
} catch (e) {
22762276
// console.error(e);
22772277
expect(e.name).to.equal('AsserterError');
2278-
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported by this asserter');
2278+
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported');
22792279
thrown = true;
22802280
}
22812281

@@ -2386,7 +2386,7 @@ describe('Asserter Tests', function () {
23862386
} catch (e) {
23872387
// console.error(e);
23882388
expect(e.name).to.equal('AsserterError');
2389-
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported by this asserter');
2389+
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported');
23902390
thrown = true;
23912391
}
23922392

@@ -2505,7 +2505,7 @@ describe('Asserter Tests', function () {
25052505
} catch (e) {
25062506
// console.error(e);
25072507
expect(e.name).to.equal('AsserterError');
2508-
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported by this asserter');
2508+
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported');
25092509
thrown = true;
25102510
}
25112511

@@ -2602,7 +2602,7 @@ describe('Asserter Tests', function () {
26022602
} catch (e) {
26032603
// console.error(e);
26042604
expect(e.name).to.equal('AsserterError');
2605-
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported by this asserter');
2605+
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported');
26062606
thrown = true;
26072607
}
26082608

@@ -2677,7 +2677,7 @@ describe('Asserter Tests', function () {
26772677
} catch (e) {
26782678
// console.error(e);
26792679
expect(e.name).to.equal('AsserterError');
2680-
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported by this asserter');
2680+
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported');
26812681
thrown = true;
26822682
}
26832683

@@ -2802,7 +2802,7 @@ describe('Asserter Tests', function () {
28022802
} catch (e) {
28032803
// console.error(e);
28042804
expect(e.name).to.equal('AsserterError');
2805-
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported by this asserter');
2805+
expect(e.message).to.equal('SupportedNetwork {"blockchain":"Bitcoin","network":"Testnet"} is not supported');
28062806
thrown = true;
28072807
}
28082808

0 commit comments

Comments
 (0)