2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ServiceResponse
|
2019-12-04 20:38:33 +05:30
|
|
|
def self.success(message: nil, payload: {}, http_status: :ok)
|
2022-10-11 01:57:18 +05:30
|
|
|
new(status: :success,
|
|
|
|
message: message,
|
|
|
|
payload: payload,
|
|
|
|
http_status: http_status)
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
def self.error(message:, payload: {}, http_status: nil, reason: nil)
|
|
|
|
new(status: :error,
|
|
|
|
message: message,
|
|
|
|
payload: payload,
|
|
|
|
http_status: http_status,
|
|
|
|
reason: reason)
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
attr_reader :status, :message, :http_status, :payload, :reason
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
def initialize(status:, message: nil, payload: {}, http_status: nil, reason: nil)
|
2019-07-31 22:56:46 +05:30
|
|
|
self.status = status
|
|
|
|
self.message = message
|
2019-09-30 21:07:59 +05:30
|
|
|
self.payload = payload
|
2019-07-31 22:56:46 +05:30
|
|
|
self.http_status = http_status
|
2022-10-11 01:57:18 +05:30
|
|
|
self.reason = reason
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def log_and_raise_exception(as: StandardError, **extra_data)
|
|
|
|
error_tracking(as) do |ex|
|
|
|
|
Gitlab::ErrorTracking.log_and_raise_exception(ex, extra_data)
|
2022-07-23 23:45:48 +05:30
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def track_exception(as: StandardError, **extra_data)
|
|
|
|
error_tracking(as) do |ex|
|
|
|
|
Gitlab::ErrorTracking.track_exception(ex, extra_data)
|
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def track_and_raise_exception(as: StandardError, **extra_data)
|
2023-03-17 16:20:25 +05:30
|
|
|
error_tracking(as) do |ex|
|
|
|
|
Gitlab::ErrorTracking.track_and_raise_exception(ex, extra_data)
|
2022-07-23 23:45:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def [](key)
|
|
|
|
to_h[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_h
|
2022-10-11 01:57:18 +05:30
|
|
|
(payload || {}).merge(
|
|
|
|
status: status,
|
|
|
|
message: message,
|
|
|
|
http_status: http_status,
|
|
|
|
reason: reason)
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def success?
|
|
|
|
status == :success
|
|
|
|
end
|
|
|
|
|
|
|
|
def error?
|
|
|
|
status == :error
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def errors
|
|
|
|
return [] unless error?
|
|
|
|
|
|
|
|
Array.wrap(message)
|
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
private
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
attr_writer :status, :message, :http_status, :payload, :reason
|
2023-03-17 16:20:25 +05:30
|
|
|
|
|
|
|
def error_tracking(error_klass)
|
|
|
|
if error?
|
|
|
|
ex = error_klass.new(message)
|
|
|
|
yield ex
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|