Re:A better way to handle errors in JavaScript
by randomdev3 on Tuesday October 31, 2023 @04:00AM
(#38081492)
Why make it complicated? The language supports catching errors, use that. You may not like it but that's the thing you have. Of course you can wrap errors, return [response,error] or whatever in your implementation of api calls etc. but you don't need third-party libraries for that.
[ Parent
| on HN ]
Re:A better way to handle errors in JavaScript
by thiht on Tuesday October 31, 2023 @04:26AM
(#38081657)
Agree with that. Exceptions suck, try-catch is an infamy, and errors not in the signatures or exploitable in any way by Typescript is a living nightmare. But what I hate even more is multiple ways to handle errors.
Re:A better way to handle errors in JavaScript
by mekster on Tuesday October 31, 2023 @06:04AM
(#38082446)
You sound like, living in a car society is a nightmare because you can't dodge cars that run into you.
Hopefully there's an ideal world for you.
Re:A better way to handle errors in JavaScript
by thiht on Tuesday October 31, 2023 @11:00AM
(#38085890)
> living in a car society is a nightmare
More like living in a society with invisible cars. You only know there was a car when it hits you. And every time you move a foot you have to first try to throw a pebble to check if there's a car
Re:A better way to handle errors in JavaScript
by musicq on Tuesday October 31, 2023 @07:29AM
(#38083152)
The thing is not using try/catch, like the first case, sometimes you don't know if a function will throw or not. You can't tell from the function signature as well.
So this provides a way that told you the function you called might throw. It's more like an alert before a crash.
[res, err] is good, actually I used this style for a long time. `unwrapit` is a nicer way to let you write [res, err], like type hints and other utility methods.
Re:A better way to handle errors in JavaScript
by golergka on Tuesday October 31, 2023 @05:01PM
(#38091315)
Because errors have different semantics. Some should kill your whole app and propagate up to the top layer, where a transaction that wraps the whole http request will be cancelled, or a error screen will be shown to the user. But other errors are part of business as usual — they should be handled explicitly, and type system should force you to do it and match the right exception type.
Personally though I would go with fp-ts.