Skip to content

Commit 3fadc66

Browse files
committed
Added overrides for library constants and logging method
1 parent dcdf574 commit 3fadc66

6 files changed

Lines changed: 50 additions & 324 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ compatible with `Error` and introduces a few new features:
3636
- Use `details`, `domain` and `tag` to customize the error object and help easily distinguish between different
3737
errors
3838

39+
## Override default log method (default is `console.log`)
40+
41+
To override the default log method, set the static property `LOG_METHOD` to a function that accepts any number of
42+
arguments and returns nothing. The default log method is `console.log`.
43+
44+
## Override default generic error code and message
45+
46+
To override the default generic error code and message, set the static properties `GENERIC_CODE` and `GENERIC_MESSAGE`.
47+
3948
## new ErrorObjectFromPayload(payload, options)
4049

4150
To parse errors from any payload, check

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@smbcheeky/error-object",
33
"description": "Create errors that can be both thrown and returned. Make error handling easier for both JavaScript and TypeScript.",
4-
"version": "1.1.9",
4+
"version": "1.2.0",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",
77
"types": "dist/index.d.ts",

playground/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,35 @@ const runFieldChecks = () => {
117117
// }
118118
};
119119

120+
const runOverrides = () => {
121+
console.log(
122+
"\n-Overrides------------------------------------------------------------------------------\n",
123+
);
124+
125+
ErrorObject.GENERIC_CODE = "specific?";
126+
ErrorObject.GENERIC_MESSAGE = "Something went :(";
127+
128+
let objectWithLogic = { complex: true };
129+
130+
ErrorObject.LOG_METHOD = () => {
131+
objectWithLogic.complex = false;
132+
};
133+
134+
console.log(objectWithLogic, "should be { complex: true }");
135+
ErrorObject.generic().log("LOG");
136+
console.log(objectWithLogic, "should be { complex: false }");
137+
138+
// { complex: true } should be { complex: true }
139+
// { complex: false } should be { complex: false }
140+
141+
ErrorObject.LOG_METHOD = null as any;
142+
ErrorObject.generic().log("LOG").debugLog("LOG").verboseLog("LOG");
143+
// should not show anything
144+
};
145+
120146
console.log("\n\n\n\n\n\n");
121147

122148
runSanityChecks();
123149
runTypeChecks();
124150
runFieldChecks();
151+
runOverrides();

playground/only-error-object.ts

Lines changed: 0 additions & 305 deletions
This file was deleted.

playground/yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"@jridgewell/sourcemap-codec" "^1.4.10"
2929

3030
"@smbcheeky/error-object@file:..":
31-
version "1.1.8"
31+
version "1.1.9"
3232
resolved "file:.."
3333

3434
"@tsconfig/node10@^1.0.7":

0 commit comments

Comments
 (0)