diff --git a/app/lib/errors.cr b/app/lib/errors.cr index 833b841..99dca4a 100644 --- a/app/lib/errors.cr +++ b/app/lib/errors.cr @@ -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" @@ -46,3 +55,12 @@ module App end end end + +error 500 do |env| + App::InternalServerErrorException.new(env) + "" +end + +error 404 do |env| + "" +end diff --git a/bit.cr b/bit.cr index f57727b..4ebd68e 100644 --- a/bit.cr +++ b/bit.cr @@ -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