Refactor error handling for API endpoints
This commit is contained in:
parent
7b53b6bfef
commit
7c75111c41
@ -33,8 +33,7 @@ class Kemal::RouteHandler
|
|||||||
raise Kemal::Exceptions::CustomException.new(context)
|
raise Kemal::Exceptions::CustomException.new(context)
|
||||||
end
|
end
|
||||||
|
|
||||||
if context.request.method == "HEAD" &&
|
if context.request.method == "HEAD" && context.request.path.ends_with? ".jpg"
|
||||||
context.request.path.ends_with? ".jpg"
|
|
||||||
context.response.headers["Content-Type"] = "image/jpeg"
|
context.response.headers["Content-Type"] = "image/jpeg"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -172,7 +171,7 @@ class APIHandler < Kemal::Handler
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
|
if env.params.query["pretty"]?.try &.== "1"
|
||||||
response = response.to_pretty_json
|
response = response.to_pretty_json
|
||||||
else
|
else
|
||||||
response = response.to_json
|
response = response.to_json
|
||||||
@ -181,6 +180,18 @@ class APIHandler < Kemal::Handler
|
|||||||
response = env.response.output.gets_to_end
|
response = env.response.output.gets_to_end
|
||||||
end
|
end
|
||||||
rescue ex
|
rescue ex
|
||||||
|
env.response.content_type = "application/json" if env.response.headers.includes_word?("Content-Type", "text/html")
|
||||||
|
env.response.status_code = 500
|
||||||
|
|
||||||
|
if env.response.headers.includes_word?("Content-Type", "application/json")
|
||||||
|
response = {"error" => ex.message || "Unspecified error"}
|
||||||
|
|
||||||
|
if env.params.query["pretty"]?.try &.== "1"
|
||||||
|
response = response.to_pretty_json
|
||||||
|
else
|
||||||
|
response = response.to_json
|
||||||
|
end
|
||||||
|
end
|
||||||
ensure
|
ensure
|
||||||
env.response.output = output
|
env.response.output = output
|
||||||
env.response.puts response
|
env.response.puts response
|
||||||
|
Loading…
Reference in New Issue
Block a user