From f4a885de5df6c5e768811c12bcae10798cbffe12 Mon Sep 17 00:00:00 2001 From: Bonflintstone Date: Thu, 29 Aug 2024 11:44:34 +0200 Subject: [PATCH] chore: Improve Error handling, consistent logging and sentry/ rollbar Imrpoves debugging experience --- CHANGELOG.md | 3 +++ box/apis/v2/accounts.rb | 6 ++++-- box/apis/v2/api_endpoint.rb | 5 ++--- box/apis/v2/management/organizations.rb | 4 ++-- box/helpers/error_handler.rb | 13 +++++++++++++ 5 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 box/helpers/error_handler.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 805d914..4b13c88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 2.2.0 +- `[ENHANCEMENT]` better error handling, more logging and sentry/ rollbar tracking + ## 2.1.1 - `[FIX]` correct broken ruby import hierarchy. This was broken in Version `2.0` - `[FIX]` add http status 500 as swagger responses diff --git a/box/apis/v2/accounts.rb b/box/apis/v2/accounts.rb index f456e76..eb468a6 100644 --- a/box/apis/v2/accounts.rb +++ b/box/apis/v2/accounts.rb @@ -70,9 +70,11 @@ class Accounts < Grape::API message: "Account created successfully. Please fetch INI letter, sign it, and submit it to your bank", account: Entities::V2::Account.represent(account) } - rescue BusinessProcesses::NewAccount::EbicsError => _ex + rescue BusinessProcesses::NewAccount::EbicsError => exception + log_error(exception) error!({message: "Failed to setup ebics_user with your bank. Make sure your data is valid and retry!"}, 412) - rescue => _ex + rescue => exception + log_error(exception) error!({message: "Failed to create account"}, 400) end diff --git a/box/apis/v2/api_endpoint.rb b/box/apis/v2/api_endpoint.rb index 4da91c3..6386cae 100644 --- a/box/apis/v2/api_endpoint.rb +++ b/box/apis/v2/api_endpoint.rb @@ -30,11 +30,10 @@ class Message < Grape::Entity version "v2", using: :header, vendor: "ebicsbox" format :json helpers Helpers::Pagination + helpers Helpers::ErrorHandler rescue_from :all do |exception| - Sentry.capture_exception(exception) if ENV["SENTRY_DSN"] - Rollbar.error(exception) if ENV["ROLLBAR_ACCESS_TOKEN"] - Box.logger.error(exception) + log_error(exception) error!({error: "Internal server error"}, 500, {"Content-Type" => "application/json"}) end diff --git a/box/apis/v2/management/organizations.rb b/box/apis/v2/management/organizations.rb index 84ba83d..814e4c0 100644 --- a/box/apis/v2/management/organizations.rb +++ b/box/apis/v2/management/organizations.rb @@ -54,8 +54,8 @@ class Organizations < Grape::API organization.add_user(declared(params)[:user].merge(admin: true)) present organization, with: Entities::V2::Organization end - rescue => ex - Box.logger.error("[Registration] #{ex.message}") + rescue => exception + log_error(exception, logger_prefix: "[Registration]") error!({message: "Failed to create organization!"}, 400) end end diff --git a/box/helpers/error_handler.rb b/box/helpers/error_handler.rb new file mode 100644 index 0000000..bcf024c --- /dev/null +++ b/box/helpers/error_handler.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Box + module Helpers + module ErrorHandler + def log_error(exception, logger_prefix: "generic") + Sentry.capture_exception(exception) if ENV["SENTRY_DSN"] + Rollbar.error(exception) if ENV["ROLLBAR_ACCESS_TOKEN"] + Box.logger.error("[#{logger_prefix}] #{exception.class} :: \"#{exception.message}\"") + end + end + end +end