Skip to content

Commit

Permalink
Remove ErrorSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
akadusei committed Feb 16, 2025
1 parent 4159b84 commit 7a894ec
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 54 deletions.
4 changes: 4 additions & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ en:
login_timed_out: Your login timed out
not_logged_in: You are not logged in
not_logged_out: You are logged in
error:
default: Something went wrong
record_not_found: Record not found
route_not_found: Not found
bearer_login:
destroy:
failure: Could not delete bearer login
Expand Down
43 changes: 13 additions & 30 deletions src/actions/errors/show.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,46 @@
#
# https://luckyframework.org/guides/http-and-routing/error-handling
class Errors::Show < Lucky::ErrorAction
DEFAULT_MESSAGE = "Something went wrong."
default_format :html
dont_report [Lucky::RouteNotFoundError]

default_format :json
disable_cookies
dont_report [Lucky::RouteNotFoundError, Avram::RecordNotFoundError]

def render(error : Lucky::RouteNotFoundError)
if html?
error_html "Sorry, we couldn't find that page.", status: 404
error_html Rex.t(:"action.error.route_not_found"), status: 404
else
error_json "Not found", status: 404
error_json message: Rex.t(:"action.error.route_not_found"), status: 404
end
end

# When the request is JSON and an InvalidOperationError is raised, show a
# helpful error with the param that is invalid, and what was wrong with it.
def render(error : Avram::InvalidOperationError)
if html?
error_html DEFAULT_MESSAGE, status: 500
error_html Rex.t(:"action.error.record_not_found"), status: 500
else
error_json \
message: error.renderable_message,
details: error.renderable_details,
param: error.invalid_attribute_name,
status: 400
error_json errors: error.errors, status: 400
end
end

# Always keep this below other 'render' methods or it may override your
# custom 'render' methods.
def render(error : Lucky::RenderableError)
if html?
error_html DEFAULT_MESSAGE, status: error.renderable_status
error_html error.renderable_message, status: error.renderable_status
else
error_json error.renderable_message, status: error.renderable_status
error_json message: error.renderable_message,
status: error.renderable_status
end
end

# If none of the 'render' methods return a response for the raised Exception,
# Lucky will use this method.
def default_render(error : Exception) : Lucky::Response
if html?
error_html DEFAULT_MESSAGE, status: 500
error_html Rex.t(:"action.error.default"), status: 500
else
error_json DEFAULT_MESSAGE, status: 500
error_json message: Rex.t(:"action.error.default"), status: 500
end
end

Expand All @@ -59,20 +54,8 @@ class Errors::Show < Lucky::ErrorAction
status_code: status
end

private def error_json(
message : String,
status : Int,
details = nil,
param = nil
)
json(
ErrorSerializer.new(
error_message: message,
details: details,
param: param
),
status: status
)
private def error_json(status, **named_args)
json FailureSerializer.new(**named_args), status: status
end

private def report(error : Exception) : Nil
Expand Down
23 changes: 0 additions & 23 deletions src/serializers/error_serializer.cr

This file was deleted.

1 change: 0 additions & 1 deletion src/serializers/mixins/serializer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Mixins::Serializer
enum Status
Success
Failure
Error
end

private abstract def status : Status
Expand Down

0 comments on commit 7a894ec

Please sign in to comment.