Skip to content

Commit 1ec1737

Browse files
committed
Add error handler for HTTP::Status and improve method definitions in DSL and Router
1 parent a3f923d commit 1ec1737

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

src/kemal/dsl.cr

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ HTTP_METHODS = %w[get post put patch delete options]
1212
FILTER_METHODS = %w[get post put patch delete options all]
1313

1414
# Defines a route for the given HTTP method.
15-
# The path must start with a `/`.
15+
#
16+
# NOTE: The path must start with a `/`.
1617
#
1718
# ```
1819
# get "/hello" do |env|
@@ -31,7 +32,8 @@ FILTER_METHODS = %w[get post put patch delete options all]
3132
{% end %}
3233

3334
# Defines a WebSocket route.
34-
# The path must start with a `/`.
35+
#
36+
# NOTE: The path must start with a `/`.
3537
#
3638
# ```
3739
# ws "/chat" do |socket, env|
@@ -56,6 +58,17 @@ def error(status_code : Int32, &block : HTTP::Server::Context, Exception -> _)
5658
Kemal.config.add_error_handler status_code, &block
5759
end
5860

61+
# Defines an error handler for the given `HTTP::Status`.
62+
#
63+
# ```
64+
# error :not_found do |env|
65+
# "Page not found"
66+
# end
67+
# ```
68+
def error(status : HTTP::Status, &block : HTTP::Server::Context, Exception -> _)
69+
Kemal.config.add_error_handler status.code, &block
70+
end
71+
5972
# Defines an error handler for the given exception type.
6073
#
6174
# ```
@@ -93,7 +106,7 @@ end
93106
Kemal::FilterHandler::INSTANCE.{{ type.id }}({{ method }}.upcase, path, &block)
94107
end
95108

96-
def {{ type.id }}_{{ method.id }}(paths : Array(String), &block : HTTP::Server::Context -> _)
109+
def {{ type.id }}_{{ method.id }}(paths : Enumerable(String), &block : HTTP::Server::Context -> _)
97110
paths.each do |path|
98111
Kemal::FilterHandler::INSTANCE.{{ type.id }}({{ method }}.upcase, path, &block)
99112
end
@@ -159,10 +172,13 @@ end
159172
# # Result: GET /users
160173
# ```
161174
def mount(router : Kemal::Router)
162-
router.register_routes("")
175+
router.register_routes
163176
end
164177

165-
# Mounts a router at the given path prefix.
178+
# Mounts a router at the given *path* prefix.
179+
#
180+
# NOTE: The path must start with a `/`.
181+
#
166182
# All routes defined in the router will be prefixed with the given path.
167183
#
168184
# ```

src/kemal/router.cr

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Kemal
22
# Router provides modular routing capabilities for Kemal applications.
33
#
44
# It allows grouping routes under a common prefix and applying filters
5-
# to specific route groups. Routers can be nested using `namespace` or `group`.
5+
# to specific route groups. Routers can be nested using `namespace`.
66
#
77
# ## Example
88
#
@@ -68,7 +68,7 @@ module Kemal
6868
end
6969

7070
# HTTP method helpers
71-
{% for method in %w[get post put patch delete options] %}
71+
{% for method in HTTP_METHODS %}
7272
# Defines a {{ method.id.upcase }} route.
7373
#
7474
# ```
@@ -117,7 +117,7 @@ module Kemal
117117
end
118118

119119
# Method-specific before/after filters
120-
{% for method in %w[get post put patch delete options all] %}
120+
{% for method in FILTER_METHODS %}
121121
# Defines a before filter for {{ method.id.upcase }} requests.
122122
def before_{{ method.id }}(path : String = "*", &block : HTTP::Server::Context -> _)
123123
add_filter(:before, {{ method.upcase }}, path, &block)
@@ -129,7 +129,10 @@ module Kemal
129129
end
130130
{% end %}
131131

132-
# Creates a nested namespace/group with the given path prefix.
132+
# Creates a nested namespace/group with the given *path* prefix.
133+
#
134+
# NOTE: The path must start with a `/`.
135+
#
133136
# All routes defined inside the block will be prefixed with the given path.
134137
#
135138
# ```
@@ -149,7 +152,9 @@ module Kemal
149152
@sub_routers << SubRouter.new(path: path, router: sub_router)
150153
end
151154

152-
# Mounts another router at the given path prefix.
155+
# Mounts another router at the given *path* prefix.
156+
#
157+
# NOTE: The path must start with a `/`.
153158
#
154159
# ```
155160
# users_router = Kemal::Router.new
@@ -207,7 +212,6 @@ module Kemal
207212
end
208213

209214
# Collect all route paths including sub-routers
210-
# :nodoc:
211215
protected def collect_all_route_paths(full_prefix : String) : Array(Tuple(String, String))
212216
paths = [] of Tuple(String, String)
213217

@@ -248,7 +252,7 @@ module Kemal
248252

249253
applicable_paths.each do |route_method, route_path|
250254
# Check if filter method matches route method
251-
next unless filter.method == "ALL" || filter.method == route_method
255+
next unless filter.method.in?("ALL", route_method)
252256

253257
# Use filter's method (ALL or specific) when registering
254258
register_method = filter.method
@@ -284,8 +288,8 @@ module Kemal
284288
end
285289

286290
private def join_paths(a : String, b : String) : String
287-
a = a.chomp("/")
288-
b = b.lchop("/") if b.starts_with?("/")
291+
a = a.chomp('/')
292+
b = b.lchop('/') if b.starts_with?('/')
289293
return "/#{b}" if a.empty?
290294
return a if b.empty?
291295
"#{a}/#{b}"

0 commit comments

Comments
 (0)