-
-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error handling proposal #233
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace api.console.content { | ||
type Code = 'ENOTFOUND' | 'EPARSE'; | ||
|
||
class CustomError extends DomainError { | ||
constructor(code?: Code); | ||
toJSON(): object; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
({ | ||
access: 'public', | ||
|
||
async method({ name }) { | ||
// Try type: new api.console.content.CustomError('EPARSE'); | ||
const filePath = `/content/${name}.md`; | ||
const buffer = application.resources.get(filePath); | ||
if (!buffer) return new Error('Content is not found'); | ||
return { text: buffer.toString() }; | ||
}, | ||
|
||
CustomError: class CustomError extends DomainError {}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace api.example.add { | ||
type Code = 'EARGA' | 'EARGB'; | ||
|
||
class CustomError extends DomainError { | ||
constructor(code?: Code); | ||
toJSON(): object; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,40 @@ | |
}, | ||
|
||
method: async ({ a, b }) => { | ||
new api.example.add.CustomError('EARGA'); | ||
if (typeof a !== 'number') return new DomainError('EARGA'); | ||
if (typeof b !== 'number') { | ||
return new api.example.example.CustomError('EARGB'); | ||
} | ||
if (Number.isNaN(a)) throw Error('Not a number: a'); | ||
if (Number.isNaN(b)) throw Error('Not a number: b'); | ||
const result = a + b; | ||
return result; | ||
}, | ||
|
||
returns: 'number', | ||
|
||
errors: { | ||
EARGA: 'Invalid argument: a', | ||
EARGB: 'Invalid argument: b', | ||
}, | ||
|
||
onError(error) { | ||
if (error.code in this.errors) { | ||
Comment on lines
+21
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is unlikely that you will know all errors that are thrown from inside the domain. Or more accurately - unlikely that you want to list them in every method. It adds a lot of chore to the api definition. |
||
console.log(`Domain error detected: ${error.code}`); | ||
} | ||
return error; | ||
}, | ||
|
||
onException(error) { | ||
console.log(`Exception throws: ${error.message}`); | ||
return error; | ||
}, | ||
|
||
CustomError: class CustomError extends DomainError { | ||
toJSON() { | ||
const { name, code, message, stack } = this; | ||
return { name, code, message, stack }; | ||
} | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,27 @@ declare global { | |
const pg: Database; | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I"d suggest to have toJSON() and toUserError() defined as global interfaces as wel. |
||
export interface ErrorOptions { | ||
code?: number | string; | ||
cause?: Error; | ||
} | ||
|
||
export class Error extends global.Error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to have some |
||
constructor(message: string, options?: number | string | ErrorOptions); | ||
message: string; | ||
stack: string; | ||
code?: number | string; | ||
cause?: Error; | ||
} | ||
|
||
type Errors = Record<string, string>; | ||
|
||
export class DomainError extends Error { | ||
constructor(code?: string, options?: number | string | ErrorOptions); | ||
message: string; | ||
stack: string; | ||
code?: number | string; | ||
cause?: Error; | ||
toError(errors: Errors): Error; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this method? |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rarely you need an error per-method.
It is much easier to manage errors defined in a separate file and exported everywhere.
This way format/code/message/interface is much easier to control.