-
Notifications
You must be signed in to change notification settings - Fork 66
/
base_error.rb
54 lines (43 loc) · 1.32 KB
/
base_error.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true
module Common
module Exceptions
# Base error class all others inherit from
class BaseError < StandardError
def errors
raise NotImplementedError, 'Subclass of Error must implement errors method'
end
def status_code
return if errors&.first.blank?
return errors.first[:status]&.to_i if errors.first.is_a?(Hash)
errors&.first&.status&.to_i
end
def message
i18n_data[:title]
end
# This determines how the exception should get logged to Sentry
# in adddition to available types from Sentry: 'warn', 'info', 'error' there is 'none' to not log to Sentry at all
def sentry_type
i18n_data[:sentry_type].presence || 'error'
end
def log_to_sentry?
sentry_type != 'none'
end
private
def i18n_key
"common.exceptions.#{self.class.name.split('::').last.underscore}"
end
def i18n_data
I18n.t(i18n_key)
end
def i18n_field(attribute, options)
I18n.t("#{i18n_key}.#{attribute}", **options)
rescue
nil
end
def i18n_interpolated(options = {})
merge_values = options.map { |attribute, opts| [attribute, i18n_field(attribute, opts)] }.to_h
i18n_data.merge(merge_values)
end
end
end
end