@@ -3,28 +3,14 @@ Grip comes with a pre-defined error handlers for the JSON response type. You can
3
3
``` ruby
4
4
class IndexController < Grip ::Controllers ::Http
5
5
def get (context : Context ) : Context
6
- raise Grip ::Exceptions ::NotFound .new
7
- end
8
- end
9
-
10
- class NotFoundController < Grip ::Controllers ::Exception
11
- # To keep the structure of the project
12
- # we still inherit from the Base class which forces us
13
- # to define the default `call` function.
14
- def call (context : Context ) : Context
15
- context
16
- .json(
17
- {
18
- " errors" => [context.exception.not_nil!.to_s]
19
- }
20
- )
6
+ raise Grip ::Exceptions ::Forbidden .new
21
7
end
22
8
end
23
9
24
10
class ForbiddenController < Grip ::Controllers ::Exception
25
11
def call (context : Context ) : Context
26
12
context
27
- .put_status(403 ) # Raised error automatically carries over the status code of the exception .
13
+ .put_status(403 ) # Raised exception automatically carries over the status code if inherited from the base class .
28
14
.json(
29
15
{
30
16
" error" => [" You lack privileges to access the current resource!" ]
@@ -38,14 +24,13 @@ class Application < Grip::Application
38
24
super (environment: " development" )
39
25
40
26
exception Grip ::Exceptions ::Forbidden , ForbiddenController
41
- exception Grip ::Exceptions ::NotFound , NotFoundController
42
-
43
- get " /" , IndexController
44
27
end
45
28
end
46
29
```
47
30
48
- Keep in mind that if you won't use the ` Exceptions ` class this will just return as a normal response:
31
+ ### Difference between native and inherited exceptions
32
+
33
+ Keep in mind that if you won't use one of the classes from the ` Exceptions ` module this will just return as a normal response:
49
34
50
35
``` ruby
51
36
class IndexController < Grip ::Controllers ::Http
71
56
You can also raise any exception you want and handle it in the error handler like this:
72
57
73
58
` ` ` ruby
74
- class ArgumentException < Grip ::Exceptions ::Base
75
- def initialize (message : String )
76
- @status_code = HTTP ::Status ::INTERNAL_SERVER_ERROR
77
- super message
59
+ class FallbackController < Grip ::Controllers ::Exception
60
+ def call (context : Context ) : Context
61
+ context.json({" error" => " An error occured, please try again later." })
78
62
end
79
63
end
80
64
81
- class IndexController < Grip ::Controllers ::Http
82
- def get (context : Context ) : Context
83
- raise ArgumentException .new (" Something went wrong" )
84
- end
85
- end
65
+ class Application < Grip ::Application
66
+ def initialize
67
+ super (environment: " development" )
86
68
87
- class BadRequestController < Grip ::Controllers ::Exception
88
- def call (context : Context ) : Context
89
- case context.exception.not_nil!
90
- when Grip ::Exceptions ::BadRequest
91
- context
92
- .json(
93
- {
94
- id: UUID .random.to_s,
95
- message: " 400 Bad request" ,
96
- }
97
- )
98
- when ArgumentException
99
- context
100
- .json(
101
- {
102
- id: UUID .random.to_s,
103
- message: " An argument error has occured, #{ context.exception.not_nil! } " ,
104
- }
105
- )
106
- else
107
- context
108
- .halt
109
- end
69
+ exception NotImplementedError , FallbackController
110
70
end
111
71
end
112
72
```
0 commit comments