/
error.cr
84 lines (73 loc) · 2.13 KB
/
error.cr
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
module Kube
class Error < Exception
class ExecutionError < Error
def initialize(command, status)
super("Command '#{command}' exited with status #{status}")
end
end
class MissingEnv < Error
def initialize(env)
super("Environment variable #{env} is not set")
end
end
class MissingConfig < Error
def initialize(config)
super("Configuration #{config} is not set")
end
end
class UndefinedResource < Error; end
class ArgumentError < Error; end
HTTP_STATUS_ERRORS = {
400 => BadRequest,
401 => Unauthorized,
403 => Forbidden,
404 => NotFound,
405 => MethodNotAllowed,
409 => Conflict,
422 => Invalid,
429 => Timeout,
500 => InternalError,
503 => ServiceUnavailable,
504 => ServerTimeout,
}
class API < Error
getter method : String
getter path : String
getter code : Int32
getter reason : String
getter status : K8S::Apimachinery::Apis::Meta::V1::Status?
private macro create_subclass
case code.code
{% for code, name in HTTP_STATUS_ERRORS %}
when {{code}}
{{name}}.new(method, path, code, nil, status)
{% end %}
else
new(method, path, code, nil, status)
end
end
def self.new(method, path, code : HTTP::Status, status : K8S::Apimachinery::Apis::Meta::V1::Status)
create_subclass
end
def initialize(@method, @path, code : HTTP::Status, reason : String?, @status = nil)
@reason = reason || code.description || "Unknown"
@code = code.code
if @status
super("#{@method} #{@path} => HTTP #{@code} #{@reason}: #{@status.not_nil!.message}")
else
super("#{@method} #{@path} => HTTP #{@code} #{@reason}")
end
end
end
macro define_api_errors
{% for code, name in HTTP_STATUS_ERRORS %}
class {{name}} < API
def initialize(method, path, code, reason, status)
super(method, path, code, reason, status)
end
end
{% end %}
end
define_api_errors
end
end