-
Notifications
You must be signed in to change notification settings - Fork 539
Expand file tree
/
Copy patherror.rb
More file actions
96 lines (81 loc) · 3.19 KB
/
error.rb
File metadata and controls
96 lines (81 loc) · 3.19 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true
module JSONAPI
class Error
attr_accessor :title, :detail, :id, :href, :code, :source, :links, :status, :meta
# Rack 3.0+ deprecated :unprocessable_entity in favor of :unprocessable_content
# This mapping ensures compatibility across Rack versions
DEPRECATED_STATUS_SYMBOLS = {
unprocessable_entity: :unprocessable_content
}.freeze
def self.status_code_for(status_symbol)
return nil if status_symbol.nil?
# Try the symbol directly first
code = Rack::Utils::SYMBOL_TO_STATUS_CODE[status_symbol]
# If not found and it's a deprecated symbol, try the new symbol
if code.nil? && DEPRECATED_STATUS_SYMBOLS.key?(status_symbol)
code = Rack::Utils::SYMBOL_TO_STATUS_CODE[DEPRECATED_STATUS_SYMBOLS[status_symbol]]
end
code&.to_s
end
def initialize(options = {})
@title = options[:title]
@detail = options[:detail]
@id = options[:id]
@href = options[:href]
@code = if JSONAPI.configuration.use_text_errors
TEXT_ERRORS[options[:code]]
else
options[:code]
end
@source = options[:source]
@links = options[:links]
@status = self.class.status_code_for(options[:status])
@meta = options[:meta]
end
def to_hash
hash = {}
instance_variables.each {|var| hash[var.to_s.delete('@')] = instance_variable_get(var) unless instance_variable_get(var).nil? }
hash
end
def update_with_overrides(error_object_overrides)
@title = error_object_overrides[:title] || @title
@detail = error_object_overrides[:detail] || @detail
@id = error_object_overrides[:id] || @id
@href = error_object_overrides[:href] || href
if error_object_overrides[:code]
# :nocov:
@code = if JSONAPI.configuration.use_text_errors
TEXT_ERRORS[error_object_overrides[:code]]
else
error_object_overrides[:code]
end
# :nocov:
end
@source = error_object_overrides[:source] || @source
@links = error_object_overrides[:links] || @links
if error_object_overrides[:status]
# :nocov:
@status = self.class.status_code_for(error_object_overrides[:status])
# :nocov:
end
@meta = error_object_overrides[:meta] || @meta
end
end
class Warning
attr_accessor :title, :detail, :code
def initialize(options = {})
@title = options[:title]
@detail = options[:detail]
@code = if JSONAPI.configuration.use_text_errors
TEXT_ERRORS[options[:code]]
else
options[:code]
end
end
def to_hash
hash = {}
instance_variables.each {|var| hash[var.to_s.delete('@')] = instance_variable_get(var) unless instance_variable_get(var).nil? }
hash
end
end
end