1 files changed, 36 insertions, 0 deletions
diff --git a/src/util/error.js b/src/util/error.js
new file mode 100644
index 0000000..96f1deb
--- /dev/null
+++ b/src/util/error.js
@@ -0,0 +1,36 @@
+export class SafeNSoundError extends Error {
+ constructor(options) {
+ super();
+ if (typeof options === 'string') {
+ this.errCode = options;
+ super.message = this.getDefaultMessage();
+ } else if (typeof options === 'object') {
+ this.errCode = options.errCode || 'UNKNOWN_ERROR';
+ super.message = options.message || this.getDefaultMessage();
+ } else {
+ this.errCode = 'UNKNOWN_ERROR';
+ this.message =
+ 'An unknown error occurred (invalid SafeNSoundError constructor options)';
+ }
+ }
+
+ getDefaultMessage() {
+ switch (this.type) {
+ case 'USER_NOT_FOUND':
+ return 'User not found';
+ case 'INVALID_CREDENTIALS':
+ return 'Invalid credentials';
+ case 'EMAIL_ALREADY_EXISTS':
+ return 'Email already exists';
+ case 'PASSWORD_TOO_WEAK':
+ return 'Password is too weak';
+ case 'TOKEN_EXPIRED':
+ return 'Token has expired';
+ case 'UNAUTHORIZED':
+ return 'Unauthorized access';
+ case 'UNKNOWN_ERROR':
+ default:
+ return 'An unknown error occurred';
+ }
+ }
+}
|