lib/sp/job/jsonapi_error.rb in sp-job-0.2.3 vs lib/sp/job/jsonapi_error.rb in sp-job-0.3.22
- old
+ new
@@ -5,90 +5,47 @@
# Copyright (c) 2017 Cloudware S.A. Allrights reserved
#
# Helper to obtain tokens to access toconline API's.
#
+require 'sp/job/common'
+
module SP
module Job
+ module JSONAPI
- #
- # Helper class to transform an error into json api format.
- #
- class JSONAPIError < StandardError
-
- # {
- # "errors": [
- # {
- # "status": nullptr,
- # "code": nullptr,
- # "detail": nullptr,
- # "meta": {
- # "internal-error": nullptr
- # }
- # }
- # ]
- # }
-
- private
-
- @error
-
- public
-
- def initialize (code: 500, detail: nil, internal: nil)
- @errors = [
- {
- :code => code,
- :detail => detail
+ class Error < ::SP::Job::Common::Exception
+ def initialize(status:, code:, detail:, internal:nil)
+ body = {
+ errors: [
+ {
+ code: "#{code}",
+ detail: "#{detail}",
+ status: "#{status} - #{::SP::Job::HttpClient.http_reason(code: status)}"
+ }
+ ]
}
- ]
- # 4xx
- case code
- when 400
- @errors[0][:status] = "Bad Request"
- when 401
- @errors[0][:status] = "Unauthorized"
- when 403
- @errors[0][:status] = "Forbidden"
- when 404
- @errors[0][:status] = "Not Found"
- when 405
- @errors[0][:status] = "Method Not Allowed"
- when 406
- @errors[0][:status] = "Not Acceptable"
- when 408
- @errors[0][:status] = "Request Timeout"
- # 5xx
- when 501
- @errors[0][:status] = "Not Implemented"
- else
- # other
- @errors[0][:status] = "Internal Server Error"
+ if nil != internal
+ body[:meta] = {
+ 'internal-error' => internal
+ }
+ end
+ super(status_code: status, content_type: 'application/vnd.api+json;charset=utf-8', body: body)
end
- @errors[0][:status] = "#{code} #{@errors[0][:status]}"
- if nil != internal
- @errors[0][:meta] = { :'internal-error' => internal }
+
+ def code
+ @body[:errors][0][:code]
end
- end
- def code ()
- return @errors[0][:code]
- end
+ def message
+ @body[:errors][0][:detail]
+ end
- def content_type ()
- "application/vnd.api+json;charset=utf-8"
- end
+ def content_type_and_body
+ [ 'application/vnd.api+json', message ]
+ end
- def body ()
- {
- :errors => @errors
- }
- end
+ end # class Error
- def content_type_and_body ()
- [ content_type(), body() ]
- end
-
- end
-
+ end # JSONAPI module
end # Job module
end # SP module