Skip to content

Commit

Permalink
fix: missing errors content type
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdonado committed Jul 14, 2024
1 parent 1d207fa commit faedd0b
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/lib/errors.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require "kemal"
module App
class BadRequestException < Kemal::Exceptions::CustomException
def initialize(context, message : String)
context.response.content_type = "application/json"
context.response.status_code = 400
context.response.print({ "error" => message }.to_json)
super(context)
Expand All @@ -11,13 +12,16 @@ module App

class UnauthorizedException < Kemal::Exceptions::CustomException
def initialize(context)
context.response.content_type = "application/json"
context.response.status_code = 401
context.response.print({ "error" => "Unauthorized access" }.to_json)
super(context)
end
end

class ForbiddenException < Kemal::Exceptions::CustomException
def initialize(context)
context.response.content_type = "application/json"
context.response.status_code = 403
context.response.print({ "error" => "Access not allowed" }.to_json)
super(context)
Expand All @@ -26,13 +30,16 @@ module App

class NotFoundException < Kemal::Exceptions::CustomException
def initialize(context)
context.response.content_type = "application/json"
context.response.status_code = 404
context.response.print({ "error" => "Resource not found" }.to_json)
super(context)
end
end

class UnprocessableEntityException < Kemal::Exceptions::CustomException
def initialize(context, message : Hash(String, Array(String)))
context.response.content_type = "application/json"
context.response.status_code = 422
context.response.print({ "errors" => message }.to_json)
super(context)
Expand Down

0 comments on commit faedd0b

Please sign in to comment.