Add custom error callback example to README
This commit is contained in:
parent
e739557f8e
commit
c970668413
1 changed files with 46 additions and 7 deletions
53
README.md
53
README.md
|
|
@ -2,12 +2,12 @@
|
||||||
|
|
||||||
## Routing rules
|
## Routing rules
|
||||||
- Static first routing
|
- Static first routing
|
||||||
- Path variables defined with `:foo`
|
- Path variables defined with colon prefix: `:foo`
|
||||||
- Example: `/odin/:version/download`
|
- Example: `/odin/:version/download`
|
||||||
- Wild card routing not implemented
|
- Wild card routing not implemented
|
||||||
|
|
||||||
## Compile time user errors included
|
## Compile time user errors included
|
||||||
When defining the server you can pass a custom error type for the server.
|
When defining the server you can pass a custom error union type for the server.
|
||||||
All the handler function will, of course, need to return these in as a pair with a response.
|
All the handler function will, of course, need to return these in as a pair with a response.
|
||||||
|
|
||||||
## Memory allocation
|
## Memory allocation
|
||||||
|
|
@ -27,6 +27,20 @@ import "core:strings"
|
||||||
import fjord "fjord"
|
import fjord "fjord"
|
||||||
import http "fjord/http"
|
import http "fjord/http"
|
||||||
|
|
||||||
|
InternalError :: enum {
|
||||||
|
DatabaseError,
|
||||||
|
}
|
||||||
|
|
||||||
|
BadRequestError :: enum {
|
||||||
|
UserUnauthorised,
|
||||||
|
InputFormatWrong,
|
||||||
|
}
|
||||||
|
|
||||||
|
Error :: union {
|
||||||
|
InternalError,
|
||||||
|
BadRequestError,
|
||||||
|
}
|
||||||
|
|
||||||
// Handler for GET /hello/:name
|
// Handler for GET /hello/:name
|
||||||
hello_handler :: proc(request: ^http.Request) -> (http.Response, Error) {
|
hello_handler :: proc(request: ^http.Request) -> (http.Response, Error) {
|
||||||
name := request.path_variables["name"]
|
name := request.path_variables["name"]
|
||||||
|
|
@ -49,7 +63,7 @@ hello_handler :: proc(request: ^http.Request) -> (http.Response, Error) {
|
||||||
// Handler for GET /users/:id/posts/:post_id
|
// Handler for GET /users/:id/posts/:post_id
|
||||||
user_post_handler :: proc(request: ^http.Request) -> (http.Response, Error) {
|
user_post_handler :: proc(request: ^http.Request) -> (http.Response, Error) {
|
||||||
user_id := request.path_variables["id"]
|
user_id := request.path_variables["id"]
|
||||||
post_id := request.path_variables["post_id"]
|
post_id := request.path_variables["post-id"]
|
||||||
|
|
||||||
body := strings.concatenate(
|
body := strings.concatenate(
|
||||||
{"<p>User ", user_id, " - Post ", post_id, "</p>"},
|
{"<p>User ", user_id, " - Post ", post_id, "</p>"},
|
||||||
|
|
@ -66,9 +80,32 @@ user_post_handler :: proc(request: ^http.Request) -> (http.Response, Error) {
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
UserError :: enum {
|
error_callback :: proc(error: Error) -> http.Response {
|
||||||
DatabaseError,
|
switch err_type in error {
|
||||||
ValidationError,
|
case BadRequestError:
|
||||||
|
switch err in err_type {
|
||||||
|
case UserUnauthorised: fallthrough
|
||||||
|
case InputFormatWrong:
|
||||||
|
body := "Bad request error"
|
||||||
|
return http.make_response(
|
||||||
|
.BadRequest,
|
||||||
|
transmute([]byte)body,
|
||||||
|
.Html,
|
||||||
|
context.temp_allocator,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
case InternalError:
|
||||||
|
switch err in err_type {
|
||||||
|
case DataBaseError:
|
||||||
|
body := "Internal server error"
|
||||||
|
return http.make_response(
|
||||||
|
.InternalServerError,
|
||||||
|
transmute([]byte)body,
|
||||||
|
.Html,
|
||||||
|
context.temp_allocator,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
|
|
@ -85,7 +122,9 @@ main :: proc() {
|
||||||
defer fjord.server_destroy(&server)
|
defer fjord.server_destroy(&server)
|
||||||
|
|
||||||
fjord.server_add_route(&server, .GET, {"hello", ":name"}, hello_handler)
|
fjord.server_add_route(&server, .GET, {"hello", ":name"}, hello_handler)
|
||||||
fjord.server_add_route(&server, .GET, {"users", ":id", "posts", ":post_id"}, user_post_handler)
|
fjord.server_add_route(&server, .GET, {"users", ":id", "posts", ":post-id"}, user_post_handler)
|
||||||
|
|
||||||
|
server_set_error_handler(&server, error_callback)
|
||||||
|
|
||||||
log.infof("Server listening on http://127.0.0.1:%d", endpoint.port)
|
log.infof("Server listening on http://127.0.0.1:%d", endpoint.port)
|
||||||
fjord.server_listen_and_serve(&server)
|
fjord.server_listen_and_serve(&server)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue