winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
4.87k stars 193 forks source link

Support Standard Structure Exceptions #6747

Open asterkin opened 2 months ago

asterkin commented 2 months ago

Use Case

Some languages, like Python https://docs.python.org/3/library/exceptions.html, provide extensive support for structured Exceptions. Wing probably does not need them all. Yet some set of standard exceptions would help. For example, a KeyError exception would mean an object is not found regardless of the underlying storage infrastructure, while ValueError would mean one or more arguments are wrong. That, in turn, would simplify mapping errors to HTTP status codes.

Proposed Solution

You may find a sample implementation here: https://github.com/asterkin/endor.w/tree/main/exception

Implementation Notes

Unline JavaScript Exception, the proposed solution does not require a new operator. On the other hand, it does not create a track trace and does not suggest any type or tag matching in the catch statement. The current implementation is 100% Wing. There are some trade-offs to be analyzed more carefully. It looks correct to keep it as a separate exception module, either part of SDK or @winglibs preserving the basing throw <string> operator.

Component

SDK, Libraries

Community Notes

Chriscbr commented 2 months ago

Thanks for opening the issue Asher. I think the right way to model errors in Wing is still an open question, and the solution might depend on how we might want to support runtime interop with other languages in the future. Right now you can only throw and catch strings in Wing but I'm not sure this is robust enough.

Complex hierarchies of exceptions like those sometimes found in Java or Python can sometimes be tedious and add a lot of mental overhead for folks learning languages in my opinion. That said, there are many cases where it's handy to be able to check for a specific type of error raised by a library or function. I think Wing could benefit from a more well-defined error type that has a simple interface (like storing a message and a stack trace, perhaps). Since Wing doesn't have a convention of functions returning "result" types like Rust, I want to say that Golang's error handling could be worth looking for as inspiration.

Related:

Chriscbr commented 2 months ago

By the way I'm unable to access your link to https://github.com/asterkin/endor.w/tree/main/exception