-
Notifications
You must be signed in to change notification settings - Fork 53
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
Make it easier to check
and return a new error with additional context
#1197
Comments
check
to return a new error value by wrapping underlying error with additional context
check
to return a new error value by wrapping underlying error with additional context check
to return a new error with additional context
This is one of the cases that
It will often be the case that multiple errors need to be augmented in the same way. I could imagine adding a langlib/stdlib function to make the construction of the new error easier. I could imagine add syntax to
|
check
to return a new error with additional context check
and return a new error with additional context
We could add an expression-oriented
desugars to
except that this is an expression. (The variable e doesn't have an explicit type by analogy with arrow functions.) |
It seems to me this is logically incorrect: Would this work? That then desugars to
Same as what you suggested but without using arrow functions and using a statement block (which must return). I guess one can argue |
@sanjiva The semantic of
With
|
Ack .. that works. I was trying to avoid using the expression function syntax. But it solves the need! |
Given that So what we are doing here is creating an expression-oriented lightweight syntax for something that can already be done more generally with a more heavy-weight statement-oriented syntax. We have a couple of cases of this already in the language
I think the syntax I have proposed is consistent with this. |
@sameerajayasoma can we get this into the next release coming in next month? It will make code people are writing less verbose and easy to read, so the faster we can get them out the better. |
@srinathperera We already passed, next month release train for language features. We can make it part of Update 5. |
@srinathperera @sameerajayasoma Are there common patterns for how the new wrapping error is created? I am wondering whether we can do anything to improve this aspect. |
@srinathperera Update 5 will be out in February last week as per the current state. @jclark There is a requirement to print the complete error, including stack traces of all causes. I am unsure whether this should be part of the langlib, but we need a utility that the log module can use as well. |
@jclark, the syntax should be
Usually, we have variables in the expression context with a |
@hasithaa Usually we do, but we don't with https://ballerina.io/spec/lang/2022R4/#infer-anonymous-function-expr which seems analogous to me. |
+1. |
<This can be ignored>For the module-level [2] we can wrap inside the generated init function.
Shall we desugar it as below?
|
Currently, we are getting a parser error for the above case because the precedence for According to the syntax |
@prakanth97 That is a serious problem, I think. We need the precedence of |
@jclark I too agree with James, Since we can't change the precedence of |
I think the way to approach this is to make the expression version of To do this with expressions, we leave
Then the semantics are:
So you can do
(assuming foo() and bar() can both return errors) Note that
makes no sense since 1 does not return an error.
will produce a semantic error if bar() returns an error (since We could potentially require that the static type of |
There can be one or more entry points in a typical real-world application: the
main
function, resource, or remote service methods. It is common to handle errors in these entry points and log them before exiting the program or sending responses to clients. Usually, many more functions are involved between the function where an error value is created and the entry point where the error is eventually handled and logged.Our recommendation so far is to use
check
to bubble up the error value to the entry point. But, it is common in real-world applications to wrap an underlying error with a new error as it bubbles up. Typically new errors are created with additional contextual details. Also, the underlying error is set as the cause so that the complete error chain can be logged with their stack traces at the entry point.Ballerina provides a way to create a new error by wrapping another with additional context. This pattern is visible in most enterprise applications written in Ballerina. e.g., WSO2 internal apps and Choreo services.
But if you use
check
, it simply returns the error to the caller.check
is concise and easy, but it's not enough in specific scenarios.I created this issue to discuss the possibility of improving the
check
to make it easy to return with a new error.The text was updated successfully, but these errors were encountered: