-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patherrors.rb
68 lines (50 loc) · 1.74 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
# frozen_string_literal: true
module MatrixSdk
# A generic error raised for issues in the MatrixSdk
class MatrixError < StandardError
end
# An error specialized and raised for failed requests
class MatrixRequestError < MatrixError
attr_reader :code, :data, :httpstatus, :message
alias error message
def self.class_by_code(code)
code = code.to_i
return MatrixNotAuthorizedError if code == 401
return MatrixForbiddenError if code == 403
return MatrixNotFoundError if code == 404
return MatrixConflictError if code == 409
return MatrixTooManyRequestsError if code == 429
MatrixRequestError
end
def self.new_by_code(data, code)
class_by_code(code).new(data, code)
end
def initialize(error, status)
@code = error[:errcode]
@httpstatus = status
@message = error[:error]
@data = error.reject { |k, _v| %i[errcode error].include? k }
super error[:error]
end
def to_s
"HTTP #{httpstatus} (#{code}): #{message}"
end
end
class MatrixNotAuthorizedError < MatrixRequestError; end
class MatrixForbiddenError < MatrixRequestError; end
class MatrixNotFoundError < MatrixRequestError; end
class MatrixConflictError < MatrixRequestError; end
class MatrixTooManyRequestsError < MatrixRequestError; end
# An error raised when errors occur in the connection layer
class MatrixConnectionError < MatrixError
def self.class_by_code(code)
return MatrixTimeoutError if code == 504
MatrixConnectionError
end
end
class MatrixTimeoutError < MatrixConnectionError
end
# An error raised when the homeserver returns an unexpected response to the client
class MatrixUnexpectedResponseError < MatrixError
end
end