-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.rb
107 lines (89 loc) · 2.73 KB
/
errors.rb
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
97
98
99
100
101
102
103
104
105
106
107
module APIv2
module ExceptionHandlers
def self.included(base)
base.instance_eval do
rescue_from Grape::Exceptions::ValidationErrors do |e|
Rack::Response.new({
error: {
code: 1001,
message: e.message
}
}.to_json, e.status)
end
end
end
end
class Error < Grape::Exceptions::Base
attr :code, :text
# code: api error code defined by Peatio, errors originated from
# subclasses of Error have code start from 2000.
# text: human readable error message
# status: http status code
def initialize(opts={})
@code = opts[:code] || 2000
@text = opts[:text] || ''
@status = opts[:status] || 400
@message = {error: {code: @code, message: @text}}
end
end
class AuthorizationError < Error
def initialize
super code: 2001, text: 'Authorization failed', status: 401
end
end
class CreateOrderError < Error
def initialize(e)
super code: 2002, text: "Failed to create order. Reason: #{e}", status: 400
end
end
class CancelOrderError < Error
def initialize(e)
super code: 2003, text: "Failed to cancel order. Reason: #{e}", status: 400
end
end
class OrderNotFoundError < Error
def initialize(id)
super code: 2004, text: "Order##{id} doesn't exist.", status: 404
end
end
class IncorrectSignatureError < Error
def initialize(signature)
super code: 2005, text: "Signature #{signature} is incorrect.", status: 401
end
end
class TonceUsedError < Error
def initialize(access_key, tonce)
super code: 2006, text: "The tonce #{tonce} has already been used by access key #{access_key}.", status: 401
end
end
class InvalidTonceError < Error
def initialize(tonce, now)
super code: 2007, text: "The tonce #{tonce} is invalid, current timestamp is #{now}.", status: 401
end
end
class InvalidAccessKeyError < Error
def initialize(access_key)
super code: 2008, text: "The access key #{access_key} does not exist.", status: 401
end
end
class DisabledAccessKeyError < Error
def initialize(access_key)
super code: 2009, text: "The access key #{access_key} is disabled.", status: 401
end
end
class ExpiredAccessKeyError < Error
def initialize(access_key)
super code: 2010, text: "The access key #{access_key} has expired.", status: 401
end
end
class OutOfScopeError < Error
def initialize
super code: 2011, text: "Requested API is out of access key scopes.", status: 401
end
end
class DepositByTxidNotFoundError < Error
def initialize(txid)
super code: 2012, text: "Deposit##txid=#{txid} doesn't exist.", status: 404
end
end
end