gun
gun copied to clipboard
Pls add error codes
Instead of or in addition to returning a string when things like user.auth() throw an error, it would be nice to have an error code. I would suggest having two error numbers, one that references the specific code and one that suggests how to handle it (ex: warn user: 1, prompt user to login again: 2, error can usually be ignored: 0). This way I don't have to copy-and-paste english strings that may change into my code.
I agree with this. It's a little unusual to need to hard code error language. That said, it would probably be a lot of work to do.
Either way, I could envision a global event driven error handler module that errors are passed to then emitted back to the developer. This could simplify coding such a change vs using error callbacks or try/catch blocks at every turn. Basically it would do something like:
gun.errors.emit('moduleName', // GUN, SEA, etc.
{ type: type, // ['error','warning','notice'],
code: n, // probably follows HTTP errors
details: new Error(text), // error constructor
})
Using the module inside other modules could be pretty simple, basically accepting errors like: gunErrorHandler(moduleName, type, code, new Error(text))
Then developers could subscribe to error events on a per module basis:
gun.errors.on('moduleName', err => {
// handle errors
})
That said, it would probably be a lot of work to do.
Why? I would imagine that instead of writing return { err: 'Message' } you could write { err: { message: 'message', type: 'warning', code: 1 }
gun.errors.on('moduleName', err => { // handle errors })
An error listener like this could be good for things like displaying notifications to a user when something fails to save, but I think we still need the errors in callback funcs, for things like reacting to invalid password.
Maybe there could be a GunError constructor which takes a code, matches it to text, calls the error handler function(s), returns an Error() with a code
property and a log
property to log it to the console. I don't think gun.errors needs to differentiate between the modules, since the modules could be differentiated by their error codes (I'm probably not making sense here)
@code3z @draeder this is great idea, and would reduce byte size slightly!!!
Tho I don't understand how i18n would work without an endpoint, wouldn't all language variations have to be hardcoded too? Searching for "GUN error 7" 😆 might turn up worse results than English...