From 957dea59c66ce2ec1d823b9e1f9da6e8607d898e Mon Sep 17 00:00:00 2001 From: FinnIckler Date: Mon, 9 Sep 2024 16:34:56 +0200 Subject: [PATCH] Add json response exceptions_app (#675) * Add json response exceptions_app * run rubocop --- app/controllers/errors_controller.rb | 10 ++++++++++ config/environments/production.rb | 5 +++++ lib/error_codes.rb | 1 + 3 files changed, 16 insertions(+) create mode 100644 app/controllers/errors_controller.rb 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