|
| 1 | +require "i18n" |
| 2 | + |
1 | 3 | module Pliny |
2 | 4 | module Errors |
3 | 5 | class Error < StandardError |
4 | | - attr_accessor :id, :metadata |
5 | 6 |
|
6 | | - def self.render(error) |
7 | | - headers = { "Content-Type" => "application/json; charset=utf-8" } |
8 | | - data = { id: error.id, message: error.message }.merge(error.metadata) |
9 | | - [error.status, headers, [MultiJson.encode(data)]] |
| 7 | + class << self |
| 8 | + attr_accessor :error_class_id, :error_class_status |
| 9 | + |
| 10 | + def render(error) |
| 11 | + headers = { "Content-Type" => "application/json; charset=utf-8" } |
| 12 | + data = { id: error.id, message: error.user_message }.merge(error.metadata) |
| 13 | + [error.status, headers, [MultiJson.encode(data)]] |
| 14 | + end |
10 | 15 | end |
11 | 16 |
|
12 | | - def initialize(message, id: nil, metadata: {}) |
13 | | - @id = id |
| 17 | + attr_accessor :id, :status, :metadata, :user_message |
| 18 | + |
| 19 | + def initialize(id=nil, metadata: {}) |
| 20 | + @id = (id || self.class.error_class_id).to_sym |
| 21 | + @status = self.class.error_class_status |
14 | 22 | @metadata = metadata |
15 | | - super(message) |
| 23 | + @user_message = I18n.t("errors.#{@id}") |
| 24 | + super(@id.to_s) |
16 | 25 | end |
17 | 26 | end |
18 | 27 |
|
19 | | - class HTTPStatusError < Error |
20 | | - attr_accessor :status |
21 | | - |
22 | | - def initialize(message=nil, options={}) |
23 | | - meta = Pliny::Errors::META[self.class] |
24 | | - message ||= "#{meta[1]}." |
25 | | - options[:id] ||= meta[1].downcase.tr(' ', '_').to_sym |
26 | | - @status = options.delete(:status) || meta[0] |
27 | | - super(message, options) |
| 28 | + def self.MakeError(status, id) |
| 29 | + Class.new(Pliny::Errors::Error) do |
| 30 | + @error_class_id = id |
| 31 | + @error_class_status = status |
28 | 32 | end |
29 | 33 | end |
30 | 34 |
|
31 | | - class BadRequest < HTTPStatusError; end # 400 |
32 | | - class Unauthorized < HTTPStatusError; end # 401 |
33 | | - class PaymentRequired < HTTPStatusError; end # 402 |
34 | | - class Forbidden < HTTPStatusError; end # 403 |
35 | | - class NotFound < HTTPStatusError; end # 404 |
36 | | - class MethodNotAllowed < HTTPStatusError; end # 405 |
37 | | - class NotAcceptable < HTTPStatusError; end # 406 |
38 | | - class ProxyAuthenticationRequired < HTTPStatusError; end # 407 |
39 | | - class RequestTimeout < HTTPStatusError; end # 408 |
40 | | - class Conflict < HTTPStatusError; end # 409 |
41 | | - class Gone < HTTPStatusError; end # 410 |
42 | | - class LengthRequired < HTTPStatusError; end # 411 |
43 | | - class PreconditionFailed < HTTPStatusError; end # 412 |
44 | | - class RequestEntityTooLarge < HTTPStatusError; end # 413 |
45 | | - class RequestURITooLong < HTTPStatusError; end # 414 |
46 | | - class UnsupportedMediaType < HTTPStatusError; end # 415 |
47 | | - class RequestedRangeNotSatisfiable < HTTPStatusError; end # 416 |
48 | | - class ExpectationFailed < HTTPStatusError; end # 417 |
49 | | - class UnprocessableEntity < HTTPStatusError; end # 422 |
50 | | - class TooManyRequests < HTTPStatusError; end # 429 |
51 | | - class InternalServerError < HTTPStatusError; end # 500 |
52 | | - class NotImplemented < HTTPStatusError; end # 501 |
53 | | - class BadGateway < HTTPStatusError; end # 502 |
54 | | - class ServiceUnavailable < HTTPStatusError; end # 503 |
55 | | - class GatewayTimeout < HTTPStatusError; end # 504 |
56 | | - |
57 | | - # Messages for nicer exceptions, from rfc2616 |
58 | | - META = { |
59 | | - BadRequest => [400, 'Bad request'], |
60 | | - Unauthorized => [401, 'Unauthorized'], |
61 | | - PaymentRequired => [402, 'Payment required'], |
62 | | - Forbidden => [403, 'Forbidden'], |
63 | | - NotFound => [404, 'Not found'], |
64 | | - MethodNotAllowed => [405, 'Method not allowed'], |
65 | | - NotAcceptable => [406, 'Not acceptable'], |
66 | | - ProxyAuthenticationRequired => [407, 'Proxy authentication required'], |
67 | | - RequestTimeout => [408, 'Request timeout'], |
68 | | - Conflict => [409, 'Conflict'], |
69 | | - Gone => [410, 'Gone'], |
70 | | - LengthRequired => [411, 'Length required'], |
71 | | - PreconditionFailed => [412, 'Precondition failed'], |
72 | | - RequestEntityTooLarge => [413, 'Request entity too large'], |
73 | | - RequestURITooLong => [414, 'Request-URI too long'], |
74 | | - UnsupportedMediaType => [415, 'Unsupported media type'], |
75 | | - RequestedRangeNotSatisfiable => [416, 'Requested range not satisfiable'], |
76 | | - ExpectationFailed => [417, 'Expectation failed'], |
77 | | - UnprocessableEntity => [422, 'Unprocessable entity'], |
78 | | - TooManyRequests => [429, 'Too many requests'], |
79 | | - InternalServerError => [500, 'Internal server error'], |
80 | | - NotImplemented => [501, 'Not implemented'], |
81 | | - BadGateway => [502, 'Bad gateway'], |
82 | | - ServiceUnavailable => [503, 'Service unavailable'], |
83 | | - GatewayTimeout => [504, 'Gateway timeout'], |
84 | | - }.freeze |
| 35 | + BadRequest = MakeError(400, :bad_request) |
| 36 | + Unauthorized = MakeError(401, :unauthorized) |
| 37 | + PaymentRequired = MakeError(402, :payment_required) |
| 38 | + Forbidden = MakeError(403, :forbidden) |
| 39 | + NotFound = MakeError(404, :not_found) |
| 40 | + MethodNotAllowed = MakeError(405, :method_not_allowed) |
| 41 | + NotAcceptable = MakeError(406, :not_acceptable) |
| 42 | + ProxyAuthenticationRequired = MakeError(407, :proxy_authentication_required) |
| 43 | + RequestTimeout = MakeError(408, :request_timeout) |
| 44 | + Conflict = MakeError(409, :conflict) |
| 45 | + Gone = MakeError(410, :gone) |
| 46 | + LengthRequired = MakeError(411, :length_required) |
| 47 | + PreconditionFailed = MakeError(412, :precondition_failed) |
| 48 | + RequestEntityTooLarge = MakeError(413, :request_entity_too_large) |
| 49 | + RequestURITooLong = MakeError(414, :request_uri_too_long) |
| 50 | + UnsupportedMediaType = MakeError(415, :unsupported_media_type) |
| 51 | + RequestedRangeNotSatisfiable = MakeError(416, :requested_range_not_satisfiable) |
| 52 | + ExpectationFailed = MakeError(417, :expectation_failed) |
| 53 | + UnprocessableEntity = MakeError(422, :unprocessable_entity) |
| 54 | + TooManyRequests = MakeError(429, :too_many_requests) |
| 55 | + InternalServerError = MakeError(500, :internal_server_error) |
| 56 | + NotImplemented = MakeError(501, :not_implemented) |
| 57 | + BadGateway = MakeError(502, :bad_gateway) |
| 58 | + ServiceUnavailable = MakeError(503, :service_unavailable) |
| 59 | + GatewayTimeout = MakeError(504, :gateway_timeout) |
85 | 60 | end |
86 | 61 | end |
0 commit comments