diff --git a/src/invidious/errors/atom.cr b/src/invidious/errors/atom.cr new file mode 100644 index 00000000..f6974336 --- /dev/null +++ b/src/invidious/errors/atom.cr @@ -0,0 +1,18 @@ +macro error_atom(*args) + error_atom_helper(env, locale, {{*args}}) +end + +def error_atom_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception) + if exception.is_a?(InfoException) + return error_atom_helper(env, locale, status_code, exception.message || "") + end + env.response.content_type = "application/atom+xml" + env.response.status_code = status_code + return "#{exception.inspect_with_backtrace}" +end + +def error_atom_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String) + env.response.content_type = "application/atom+xml" + env.response.status_code = status_code + return "#{message}" +end diff --git a/src/invidious/errors/errors.cr b/src/invidious/errors/errors.cr index 98642bf7..f970e234 100644 --- a/src/invidious/errors/errors.cr +++ b/src/invidious/errors/errors.cr @@ -46,60 +46,6 @@ def error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSO return templated "error" end -macro error_atom(*args) - error_atom_helper(env, locale, {{*args}}) -end - -def error_atom_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception) - if exception.is_a?(InfoException) - return error_atom_helper(env, locale, status_code, exception.message || "") - end - env.response.content_type = "application/atom+xml" - env.response.status_code = status_code - return "#{exception.inspect_with_backtrace}" -end - -def error_atom_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String) - env.response.content_type = "application/atom+xml" - env.response.status_code = status_code - return "#{message}" -end - -macro error_json(*args) - error_json_helper(env, locale, {{*args}}) -end - -def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception, additional_fields : Hash(String, Object) | Nil) - if exception.is_a?(InfoException) - return error_json_helper(env, locale, status_code, exception.message || "", additional_fields) - end - env.response.content_type = "application/json" - env.response.status_code = status_code - error_message = {"error" => exception.message, "errorBacktrace" => exception.inspect_with_backtrace} - if additional_fields - error_message = error_message.merge(additional_fields) - end - return error_message.to_json -end - -def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception) - return error_json_helper(env, locale, status_code, exception, nil) -end - -def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String, additional_fields : Hash(String, Object) | Nil) - env.response.content_type = "application/json" - env.response.status_code = status_code - error_message = {"error" => message} - if additional_fields - error_message = error_message.merge(additional_fields) - end - return error_message.to_json -end - -def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String) - error_json_helper(env, locale, status_code, message, nil) -end - def error_redirect_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil) request_path = env.request.path diff --git a/src/invidious/errors/json.cr b/src/invidious/errors/json.cr new file mode 100644 index 00000000..d9294c7e --- /dev/null +++ b/src/invidious/errors/json.cr @@ -0,0 +1,34 @@ +macro error_json(*args) + error_json_helper(env, locale, {{*args}}) +end + +def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception, additional_fields : Hash(String, Object) | Nil) + if exception.is_a?(InfoException) + return error_json_helper(env, locale, status_code, exception.message || "", additional_fields) + end + env.response.content_type = "application/json" + env.response.status_code = status_code + error_message = {"error" => exception.message, "errorBacktrace" => exception.inspect_with_backtrace} + if additional_fields + error_message = error_message.merge(additional_fields) + end + return error_message.to_json +end + +def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception) + return error_json_helper(env, locale, status_code, exception, nil) +end + +def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String, additional_fields : Hash(String, Object) | Nil) + env.response.content_type = "application/json" + env.response.status_code = status_code + error_message = {"error" => message} + if additional_fields + error_message = error_message.merge(additional_fields) + end + return error_message.to_json +end + +def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String) + error_json_helper(env, locale, status_code, message, nil) +end