diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb new file mode 100644 index 00000000..e38367f2 --- /dev/null +++ b/app/controllers/errors_controller.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class ErrorsController < ApplicationController + def show + exception = request.env['action_dispatch.exception'] + status_code = ActionDispatch::ExceptionWrapper.new(request.env, exception).status_code + request_id = request.env['action_dispatch.request_id'] + render json: { error: ErrorCodes::INTERNAL_SERVER_ERROR, request_id: request_id }, status: status_code + end +end diff --git a/config/environments/production.rb b/config/environments/production.rb index 066ddffc..d18e6795 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -98,4 +98,9 @@ # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false + + # Returns json on error + config.exceptions_app = ->(env) { + ErrorsController.action(:show).call(env) + } end diff --git a/lib/error_codes.rb b/lib/error_codes.rb index b42cb109..a4da53b5 100644 --- a/lib/error_codes.rb +++ b/lib/error_codes.rb @@ -5,6 +5,7 @@ module ErrorCodes INVALID_TOKEN = -1 EXPIRED_TOKEN = -2 MISSING_AUTHENTICATION = -3 + INTERNAL_SERVER_ERROR = -4 # Competition Errors COMPETITION_NOT_FOUND = -1000