Skip to content

Commit

Permalink
fix: error handling override kemal default response
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdonado committed Jul 14, 2024
1 parent faedd0b commit 2f14cd8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 18 additions & 0 deletions app/lib/errors.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
require "kemal"

module App
class InternalServerErrorException < Kemal::Exceptions::CustomException
def initialize(context)
context.response.content_type = "application/json"
context.response.status_code = 500
context.response.print({ "error" => "Internal Server Error" }.to_json)
super(context)
end
end

class BadRequestException < Kemal::Exceptions::CustomException
def initialize(context, message : String)
context.response.content_type = "application/json"
Expand Down Expand Up @@ -46,3 +55,12 @@ module App
end
end
end

error 500 do |env|
App::InternalServerErrorException.new(env)
""
end

error 404 do |env|
""
end
4 changes: 0 additions & 4 deletions bit.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@ require "./app/routes"
add_context_storage_type(App::Models::User)
add_handler(App::Middlewares::Auth.new)

error 500 { |env| {"error" => "Internal Server Error" }.to_json}
error 401 { |env| {"error" => "Unauthorized" }.to_json}
error 404 { |env| {"error" => "Not Found" }.to_json}

Kemal.run

0 comments on commit 2f14cd8

Please sign in to comment.