Class: ServiceResponse

Inherits:
Object
  • Object
show all
Defined in:
app/services/service_response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, message: nil, payload: {}, http_status: nil, reason: nil) ⇒ ServiceResponse

Returns a new instance of ServiceResponse.



33
34
35
36
37
38
39
# File 'app/services/service_response.rb', line 33

def initialize(status:, message: nil, payload: {}, http_status: nil, reason: nil)
  self.status = status
  self.message = message
  self.payload = payload
  self.http_status = http_status
  self.reason = reason
end

Instance Attribute Details

#http_statusObject

Returns the value of attribute http_status.



31
32
33
# File 'app/services/service_response.rb', line 31

def http_status
  @http_status
end

#messageObject

Returns the value of attribute message.



31
32
33
# File 'app/services/service_response.rb', line 31

def message
  @message
end

#payloadObject

Returns the value of attribute payload.



31
32
33
# File 'app/services/service_response.rb', line 31

def payload
  @payload
end

#reasonObject

Returns the value of attribute reason.



31
32
33
# File 'app/services/service_response.rb', line 31

def reason
  @reason
end

#statusObject

Returns the value of attribute status.



31
32
33
# File 'app/services/service_response.rb', line 31

def status
  @status
end

Class Method Details

.error(message:, payload: {}, http_status: nil, reason: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'app/services/service_response.rb', line 13

def self.error(message:, payload: {}, http_status: nil, reason: nil)
  new(
    status: :error,
    message: message,
    payload: payload,
    http_status: http_status,
    reason: reason
  )
end

.from_legacy_hash(response) ⇒ Object

This is used to help wrap old service responses that were just hashes

Raises:

  • (ArgumentError)


24
25
26
27
28
29
# File 'app/services/service_response.rb', line 24

def self.from_legacy_hash(response)
  return response if response.is_a?(ServiceResponse)
  return ServiceResponse.new(**response) if response.is_a?(Hash)

  raise ArgumentError, "argument must be a ServiceResponse or a Hash"
end

.success(message: nil, payload: {}, http_status: :ok) ⇒ Object



4
5
6
7
8
9
10
11
# File 'app/services/service_response.rb', line 4

def self.success(message: nil, payload: {}, http_status: :ok)
  new(
    status: :success,
    message: message,
    payload: payload,
    http_status: http_status
  )
end

Instance Method Details

#[](key) ⇒ Object



59
60
61
# File 'app/services/service_response.rb', line 59

def [](key)
  to_h[key]
end

#causeObject



89
90
91
# File 'app/services/service_response.rb', line 89

def cause
  ActiveSupport::StringInquirer.new(reason.to_s)
end

#deconstruct_keys(keys) ⇒ Object



71
72
73
# File 'app/services/service_response.rb', line 71

def deconstruct_keys(keys)
  to_h.slice(*keys)
end

#error?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/services/service_response.rb', line 79

def error?
  status == :error
end

#errorsObject



83
84
85
86
87
# File 'app/services/service_response.rb', line 83

def errors
  return [] unless error?

  Array.wrap(message)
end

#log_and_raise_exception(as: StandardError, **extra_data) ⇒ Object



41
42
43
44
45
# File 'app/services/service_response.rb', line 41

def log_and_raise_exception(as: StandardError, **extra_data)
  error_tracking(as) do |ex|
    Gitlab::ErrorTracking.log_and_raise_exception(ex, extra_data)
  end
end

#success?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/services/service_response.rb', line 75

def success?
  status == :success
end

#to_hObject



63
64
65
66
67
68
69
# File 'app/services/service_response.rb', line 63

def to_h
  (payload || {}).merge(
    status: status,
    message: message,
    http_status: http_status,
    reason: reason)
end

#track_and_raise_exception(as: StandardError, **extra_data) ⇒ Object



53
54
55
56
57
# File 'app/services/service_response.rb', line 53

def track_and_raise_exception(as: StandardError, **extra_data)
  error_tracking(as) do |ex|
    Gitlab::ErrorTracking.track_and_raise_exception(ex, extra_data)
  end
end

#track_exception(as: StandardError, **extra_data) ⇒ Object



47
48
49
50
51
# File 'app/services/service_response.rb', line 47

def track_exception(as: StandardError, **extra_data)
  error_tracking(as) do |ex|
    Gitlab::ErrorTracking.track_exception(ex, extra_data)
  end
end