Skip to content

Commit 7ff594e

Browse files
author
Lukas Siemon
committed
feat: added error context and message id to build in api errors
1 parent 0d613b6 commit 7ff594e

6 files changed

Lines changed: 11 additions & 8 deletions

File tree

lib/api.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ const Parser = (request, params) => {
2929
try {
3030
body = JSON.parse(get(event, 'body', '{}'));
3131
} catch (e) {
32-
throw response.ApiError("Invalid Json Body detected.");
32+
throw response.ApiError("Invalid Json Body detected.", 400, 99001, {
33+
value: get(event, 'body')
34+
});
3335
}
3436
const eventParsed = Object.assign({}, event, { body });
3537
params.forEach(p => p.feed(eventParsed));

lib/param.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ class Param {
2929
const result = get(this.event, `${types[this.type]}.${this.name}`);
3030
if (result === undefined) {
3131
if (this.required) {
32-
throw response.ApiError(`Required ${this.type}-Parameter "${this.name}" missing.`);
32+
throw response.ApiError(`Required ${this.type}-Parameter "${this.name}" missing.`, 400, 99002);
3333
}
3434
} else if (!this.validate(result)) {
35-
// todo: add context
36-
throw response.ApiError(`Invalid Value for ${this.type}-Parameter "${this.name}" provided.`);
35+
throw response.ApiError(`Invalid Value for ${this.type}-Parameter "${this.name}" provided.`, 400, 99003, {
36+
value: result
37+
});
3738
}
3839
return result;
3940
}

test/handler/test_param_invalid_body.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"timeout": 2000,
55
"response": [
66
{
7-
"to.deep.equal": "{\"statusCode\":400,\"body\":\"{\\\"message\\\":\\\"Invalid Json Body detected.\\\"}\"}"
7+
"to.deep.equal": "{\"statusCode\":400,\"body\":\"{\\\"message\\\":\\\"Invalid Json Body detected.\\\",\\\"messageId\\\":99001,\\\"context\\\":{\\\"value\\\":\\\"invalid_json_string\\\"}}\"}"
88
}
99
],
1010
"logs": [

test/handler/test_param_invalid_email.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"timeout": 2000,
55
"response": [
66
{
7-
"to.deep.equal": "{\"statusCode\":400,\"body\":\"{\\\"message\\\":\\\"Invalid Value for json-Parameter \\\\\\\"email\\\\\\\" provided.\\\"}\"}"
7+
"to.deep.equal": "{\"statusCode\":400,\"body\":\"{\\\"message\\\":\\\"Invalid Value for json-Parameter \\\\\\\"email\\\\\\\" provided.\\\",\\\"messageId\\\":99003,\\\"context\\\":{\\\"value\\\":\\\"invalid@email\\\"}}\"}"
88
}
99
],
1010
"logs": [

test/handler/test_param_missing_required.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"timeout": 2000,
55
"response": [
66
{
7-
"to.deep.equal": "{\"statusCode\":400,\"body\":\"{\\\"message\\\":\\\"Required json-Parameter \\\\\\\"username\\\\\\\" missing.\\\"}\"}"
7+
"to.deep.equal": "{\"statusCode\":400,\"body\":\"{\\\"message\\\":\\\"Required json-Parameter \\\\\\\"username\\\\\\\" missing.\\\",\\\"messageId\\\":99002}\"}"
88
}
99
],
1010
"logs": [

test/handler/test_param_not_string.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"timeout": 2000,
55
"response": [
66
{
7-
"to.deep.equal": "{\"statusCode\":400,\"body\":\"{\\\"message\\\":\\\"Invalid Value for json-Parameter \\\\\\\"username\\\\\\\" provided.\\\"}\"}"
7+
"to.deep.equal": "{\"statusCode\":400,\"body\":\"{\\\"message\\\":\\\"Invalid Value for json-Parameter \\\\\\\"username\\\\\\\" provided.\\\",\\\"messageId\\\":99003,\\\"context\\\":{\\\"value\\\":{}}}\"}"
88
}
99
],
1010
"logs": [

0 commit comments

Comments
 (0)