Skip to content

Instantly share code, notes, and snippets.

@gcoonrod
Created September 12, 2020 14:46
Show Gist options
  • Save gcoonrod/69e5025b3e8822dfdd70f0c8e555118e to your computer and use it in GitHub Desktop.
Save gcoonrod/69e5025b3e8822dfdd70f0c8e555118e to your computer and use it in GitHub Desktop.
Response logging with error serializer
diff --git a/src/actions/errors.ts b/src/actions/errors.ts
new file mode 100644
index 00000000..163eda37
--- /dev/null
+++ b/src/actions/errors.ts
@@ -0,0 +1,31 @@
+import { Action } from "./../index"
+
+export class TestError extends Action {
+ constructor() {
+ super();
+ this.name = "testErr";
+ this.description = "I am an API method which will generate a random number";
+ this.outputExample = { randomNumber: 0.123 };
+ }
+
+ async run({ connection, response }) {
+ throw new Error("some internal error");
+ }
+}
+
+export class TestAppError extends Action {
+ constructor() {
+ super();
+ this.name = "testAppErr";
+ this.description = "I am an API method which will generate a random number";
+ this.outputExample = { randomNumber: 0.123 };
+ }
+
+ async run({ connection, response }) {
+ const err = new Error("some app error");
+ err.name = "AppError";
+ // @ts-ignore
+ err.appCode = "100500";
+ throw err;
+ }
+}
diff --git a/src/config/api.ts b/src/config/api.ts
index b079b21b..fff480f3 100644
--- a/src/config/api.ts
+++ b/src/config/api.ts
@@ -22,7 +22,7 @@ export const DEFAULT = {
// disables the whitelisting of client params
disableParamScrubbing: false,
// enable action response to logger
- enableResponseLogging: false,
+ enableResponseLogging: true,
// params you would like hidden from any logs
filteredParams: [],
// responses you would like hidden from any logs
diff --git a/src/config/errors.ts b/src/config/errors.ts
index f7fc6966..1b477100 100644
--- a/src/config/errors.ts
+++ b/src/config/errors.ts
@@ -10,11 +10,16 @@ export const DEFAULT = {
serializers: {
servers: {
web: (error) => {
- if (error.message) {
- return String(error.message);
- } else {
- return error;
+ // if (error.message) {
+ // return String(error.message);
+ // } else {
+ // return error;
+ // }
+ if (error.name === "AppError") {
+ return { code: error.appCode, message: error.message };
}
+
+ return { code: 500, message: "ISE" };
},
websocket: (error) => {
if (error.message) {
diff --git a/src/config/routes.ts b/src/config/routes.ts
index 1d5c51b1..5bddba89 100644
--- a/src/config/routes.ts
+++ b/src/config/routes.ts
@@ -25,6 +25,10 @@ export const DEFAULT = {
]
---------------------- */
+ get: [
+ { path: '/testError', action: 'testErr' },
+ { path: '/testAppError', action: 'testAppErr' }
+ ]
};
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment