-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
Counter proposal : type safe try/catch #43
Comments
@nicolo-ribaudo indeed it is! the syntax feels a bit odd to me because of those 2 reasons:
Alternatively, having: try {
cons res = await fetch("https://api.example.com/data")
return validationSchema.parse(
await res.json()
)
}
when RequestError catch (err) {
handleRequestError(err)
} ...could also work, but idk, |
“on” is a word that’s associated with event listeners and observables; i don’t think that makes sense. The pattern matching proposal may add an |
@ljharb i don't have strong opinion on it. the difference imho, is in where the keyword is located. with |
What about Python-like style try {
cons res = await fetch("https://api.example.com/data")
return validationSchema.parse(
await res.json()
)
}
except RequestError (error) { } |
Of course, this violates duck typing principles as now the "type" of the error has meaning. |
@bruno-brant there's a world beyond typescript. |
@y-nk "duck typing" has nothing to do with typescript, and being able to throw any value is neither a side effect nor sad, it's a critical functionality that every piece of the language must always design to include. Additionally, |
@y-nk not sure I understand your remark, as the proposal is for JavaScript, and I've never even mentioned TS. And the fact that you can test the prototype of an object does not mean that we should forget that JavaScript was conceived as a duck typed language. Back to your point, you show a sample of handling errors in the fetch API. I take that is a fictional example, as I've not found any documentation out there defining that those classes will be thrown. Even if they are thrown - you might've confirmed that in code - if they aren't documented, there's nothing preventing it from changing tomorrow from ParseError to JsonError, which would break your code. So, it has always been my understanding that in Javascript testing a type of an object is a bad practice that can lead to errors. |
You are definitely right about the issues with different realms and the possibility of faking using Only a small number of libraries provide error-detecting predicates, and an even smaller number identify their own exceptions without relying on Therefore, using Perhaps only Node.js is the exception, as it provides a wide set of error codes for different cases. But in my own practice I'n not identifying them at all, just catching in the error boundary and reporting to sentry. But for error codes, it's better to use a |
“we already have a large codebase doing a bad thing” is not a persuasive argument to me to cargocult something into the language. In TS, a tuple can be named just like a record/interface/object, and we should ne designing things based on what makes the most sense for JavaScript, not for a totally different language. |
@ljharb , I'm not sure I got your idea correctly. But when I talked about a large codebase, About cargocult... EcmaScript borrows a lot from other languages (classes, generators, even arrow The Result type is not a replacement for exceptions; it just works better |
Wide usage never precludes something being a bad practice, and the issues with instanceof aren’t edge cases. |
The proposal seems copying Golang famous
value, _ := blah()
but I think we can do better in error handling if we copied dart/java instead.Considering the 1st example:
I would rather leverage a new
on XXX catch (err)
instead which would look like this:Advantages over your proposal:
EDIT: this addresses your concerns about nested try/catch
EDIT2: would consider also
rethrow
as a syntaxic sugar ofthrow err
, that'd be nice but it's not really inside the proposalThe text was updated successfully, but these errors were encountered: