Guidelines
Errors
Errors and Form Validations Guidelines
Go
- We follow the rule of handling an error only once, which means you either return the error or log it, but never do both.
- If more context is needed, wrap the error using
fmt.Errorf("...: %w")
. - Services must always return system errors (from
system/errors.go
). The handler then unwraps the error and, based on the type, applies the proper logic. - If it’s a validation error that we want to display on forms, we must return
system.ErrValidation
with the appropriate error code:- HTTP - Unprocessed Entity 422
- gRPC - Invalid Argument 3
- ALL ERRORS are checked.
SvelteKit / NextJS
- We use a wrapper around
try / catch
that forces you to handle errors. It aims to mimic what Go or Rust do (tries, JS error handling sucks). - The wrapper is called
Safe
, and you can find its logic insafe.ts
file. For more information, please check the article: Typescript with Go/Rust Errors: No Try/Catch Heresy - For form validation, there are two approaches to handling validations:
- Basic Native HTML + Full Server-Side: This is the recommended approach as it removes as much unnecessary logic from the frontend as possible.
Basic validation is performed using native HTML methods (e.g.,
required
,maxlength
), while more advanced validations are handled by the server. When the server sends a proper status code, the frontend unwraps the message, assigns it to thefields
object, and passes it down to the pages. The page then extracts the errors and displays it on the form. - Full Client-Side + Full Server-Side: This approach uses the
zod
library for client-side validation. TheEdit Note
page is the only one that showcases this flow. It works similarly to the Full Server-Side approach, with the exception thatzod
handles input validation first and then passes thefields
object down. Of course, the server must still perform its full validation.
Need help?
Visit our discord server to ask any questions, make suggestions and give feedback :).
Was this page helpful?