Suvidha’s Http classes simplify handling responses. The default handlers support both Http classes and plain Protocol objects.
If you write your own handlers, you must support these classes to use them effectively.
The Protocol Type
Response structure:
Using the Http Classes
The Http.End Class (Base Class)
This serves as the foundation for all other Http classes.
HTTP Response Classes
2xx (Success) classes require a body. Other classes (1xx, 3xx, 4xx, 5xx) have default messages.
Http.Ok (200 OK)
- Description: Represents a successful 200 OK response.
- Usage:
Http.Ok.body(body).
- Example:
return Http.Ok.body({ data: result });.
Http.BadRequest (400 Bad Request)
- Description: Represents a 400 Bad Request response.
- Usage:
throw new Http.BadRequest.body(body) or throw new Http.BadRequest() (for default message).
- Example:
throw new Http.BadRequest.body({ error: "Invalid input" }); or throw new Http.BadRequest(); (sends “Bad Request”).
Other classes follow the same pattern.
Error Handling with Http Classes
You can throw an instance of the appropriate Http error class.
Simple Alternative
You can return plain Protocol objects if preferred:
Do not throw plain Protocol objects, as they are not processed by the
onErr handler in DefaultHandlers.