File tree 1 file changed +28
-0
lines changed 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 73
73
- [ Before and After] ( #before-and-after )
74
74
- [ Anchoring] ( #anchoring )
75
75
- [ Using Custom Middleware] ( #using-custom-middleware )
76
+ - [ Grape Middleware] ( #grape-middleware )
76
77
- [ Rails Middleware] ( #rails-middleware )
77
78
- [ Remote IP] ( #remote-ip )
78
79
- [ Writing Tests] ( #writing-tests )
@@ -2557,6 +2558,33 @@ part.
2557
2558
2558
2559
## Using Custom Middleware
2559
2560
2561
+ ### Grape Middleware
2562
+
2563
+ You can make a custom middleware by using ` Grape::Middleware::Base ` .
2564
+ It's inherited from some grape official middlewares in fact.
2565
+
2566
+ For example, you can write a middleware to log application exception.
2567
+
2568
+ ``` ruby
2569
+ class LoggingError < Grape ::Middleware ::Base
2570
+ def after
2571
+ if @api_response [0 ] == 500
2572
+ env[' rack.logger' ].error(" Raised error on #{ env[' PATH_INFO' ] } " )
2573
+ end
2574
+ end
2575
+ end
2576
+ ```
2577
+
2578
+ Your middleware can overwrite application response as follows, except error case.
2579
+
2580
+ ``` ruby
2581
+ class Overwriter < Grape ::Middleware ::Base
2582
+ def after
2583
+ [200 , { ' Content-Type' => ' text/plain' }, [' Overwrited.' ]]
2584
+ end
2585
+ end
2586
+ ```
2587
+
2560
2588
### Rails Middleware
2561
2589
2562
2590
Note that when you're using Grape mounted on Rails you don't have to use Rails middleware because it's already included into your middleware stack.
You can’t perform that action at this time.
0 commit comments