Generating Dedicated Error Structs for All Global Errors? #3733
-
I'm working with Goa v3 and defining a set of global errors at the API level to simplify error handling across my services, as shown in the documentation (e.g., goa.design/docs/3-tutorials/3-error-handling/2-defining-errors/). My design.go structure for global errors:
Problem: When generating code (goa gen my_module_path/design), for errors like not_found (where I haven't specified an explicit Type()), Goa generates Make helper functions (e.g., MakeNotFound). However, returning these errors in my implementation feels verbose:
I would prefer to have a dedicated, generated Go struct for each of my global errors, allowing for more concise and type-safe returns, even if these structs only contain a Message field. Desired Implementation Outcome: I want to be able to do something like this in my implementation:
What is the idiomatic or best-practice way in Goa v3 to define global errors in the DSL such that goa gen produces dedicated Go structs for each of them (e.g., type NotFound struct { Message string }) that I can instantiate and return directly, instead of relying on goa.NewServiceError or Make helpers for generic goa.ServiceErrors? Thank you for your guidance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Goa generates dedicated constructor methods for errors, see for example https://github.com/goadesign/examples/blob/main/error/gen/calc/service.go#L81-L84. Does that scratch the itch? |
Beta Was this translation helpful? Give feedback.
Goa generates dedicated constructor methods for errors, see for example https://github.com/goadesign/examples/blob/main/error/gen/calc/service.go#L81-L84. Does that scratch the itch?