Skip to content

Commit 706532e

Browse files
Giorgi KavrelishviliGiorgi Kavrelishvili
authored andcommitted
Update documentation
1 parent 67e11ec commit 706532e

File tree

1 file changed

+12
-52
lines changed

1 file changed

+12
-52
lines changed

docs/error_handling.md

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,14 @@ Grip comes with a pre-defined error handlers for the JSON response type. You can
33
```ruby
44
class IndexController < Grip::Controllers::Http
55
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
217
end
228
end
239

2410
class ForbiddenController < Grip::Controllers::Exception
2511
def call(context : Context) : Context
2612
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.
2814
.json(
2915
{
3016
"error" => ["You lack privileges to access the current resource!"]
@@ -38,14 +24,13 @@ class Application < Grip::Application
3824
super(environment: "development")
3925

4026
exception Grip::Exceptions::Forbidden, ForbiddenController
41-
exception Grip::Exceptions::NotFound, NotFoundController
42-
43-
get "/", IndexController
4427
end
4528
end
4629
```
4730

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:
4934

5035
```ruby
5136
class IndexController < Grip::Controllers::Http
@@ -71,42 +56,17 @@ end
7156
You can also raise any exception you want and handle it in the error handler like this:
7257
7358
```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."})
7862
end
7963
end
8064

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")
8668

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
11070
end
11171
end
11272
```

0 commit comments

Comments
 (0)