-
Notifications
You must be signed in to change notification settings - Fork 87
/
error.jl
77 lines (65 loc) · 2.1 KB
/
error.jl
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
# Copyright (c) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
"""
UnsupportedError <: Exception
Abstract type for error thrown when an element is not supported by the model.
"""
abstract type UnsupportedError <: Exception end
"""
element_name(err::UnsupportedError)
Return the name of the element that is not supported.
"""
function element_name end
function Base.showerror(io::IO, err::UnsupportedError)
print(
io,
typeof(err),
": ",
element_name(err),
" is not supported by the model",
)
m = message(err)
if Base.isempty(m)
print(io, ".")
else
print(io, ": ", m)
end
end
"""
NotAllowedError <: Exception
Abstract type for error thrown when an operation is supported but cannot be
applied in the current state of the model.
"""
abstract type NotAllowedError <: Exception end
"""
operation_name(err::NotAllowedError)
Return the name of the operation throwing the error in a gerund (that is, -ing
form).
"""
function operation_name end
function Base.showerror(io::IO, err::NotAllowedError)
print(io, typeof(err), ": ", operation_name(err), " cannot be performed")
m = message(err)
if Base.isempty(m)
print(io, ".")
else
print(io, ": ", m)
end
return print(
io,
" You may want to use a `CachingOptimizer` in `AUTOMATIC` mode",
" or you may need to call `reset_optimizer` before doing this",
" operation if the `CachingOptimizer` is in `MANUAL` mode.",
)
end
"""
message(err::Union{UnsupportedError, NotAllowedError})
Return a `String` containing a human-friendly explanation of why the operation
is not supported/cannot be performed. It is printed in the error message if it
is not empty. By convention, it should be stored in the `message` field; if
this is the case, the `message` method does not have to be implemented.
"""
message(err::Union{UnsupportedError,NotAllowedError}) = err.message