Comments (1)
The main differences between v0.28.3 and v1.0.0 are:
-
New controller types for specific handlers, for example:
An error handler is now a controller,
A filter handler is now a controller. -
New DSL entries for the router:
An error DSL which adds the error controller to the error handler list,
A filter DSL which adds the filter controller to the filter handler list. -
Parameter parser has new parameters being parsed:
File upload is now possible with Grip,
Form data parsing is now possible with Grip. -
Documentation:
Documented modules and added some information about the functions and classes. -
Refactoring:
Renamed couple of classes, updated the exception handler, updated the exception base class and fixed issues with WebSockets.
There are other changes as well but these are the main ones which need a bit of attention since some of the old codebase might not work in the new version.
Also there is a cookiecutter template which defines the structure of a Grip project and how everything should be handled. It doesn't enforce the style, it is just a recommendation for people who struggle with creating a structure for a web app.
The structure defines exception handlers by specifying the error code and when an exception is thrown which inherits the base class of the Grip::Exceptions::Base
and has a defined status code, the exception handler matches over the status code and calls the defined status code handler with which it renders a specific error view.
For example:
require "grip"
class UserNotFound < Grip::Exceptions::Base
def initialize(id)
@status = HTTP::Status::NOT_FOUND
super "User with an ID of #{id} was not found!"
end
def status_code : Int32
@status.not_nil!.value
end
end
class DatabaseConnection
def get(id)
raise UserNotFound.new(id)
end
end
class NotFoundView
def self.render_default(context)
context.response.headers.merge!({"Content-Type" => "application/json"})
context.response.print(
{
"errors" => [
context.exception.to_s,
],
}.to_json
)
end
def self.render_user_not_found(context)
context.response.headers.merge!({"Content-Type" => "application/json"})
context.response.print(
{
"code" => 13,
"errors" => [
context.exception.to_s,
],
}.to_json
)
end
end
class NotFoundController < Grip::Controllers::Exception
def call(context)
case context.exception.not_nil!
when Grip::Exceptions::NotFound
NotFoundView.render_default(context)
when UserNotFound
NotFoundView.render_user_not_found(context)
end
end
end
class UserController < Grip::Controllers::Http
def get(context)
db = DatabaseConnection.new
params = url?(context)
json!(
context,
db.get(
params["id"]
)
)
end
end
class Application < Grip::Application
def initialize
error 404, NotFoundController
get "/:id", UserController
end
end
app = Application.new
app.run
I think this creates better consistency throughout the entire project and gives you an option to return responses via the exception details and types, so the developer or the client of your API will struggle less to understand what went wrong.
Take a look at this example project which has the current structure defined.
from grip.
Related Issues (20)
- Undefined method 'final' for OpenSSL::Digest HOT 1
- Grip::Pipes::Log show/print only HTTP status 200 requests HOT 5
- Rendering HTML does not have a default encoding HOT 8
- TechEmpower benchmark is broken HOT 1
- Built in static asset server HOT 5
- Custom error handling and compile error HOT 1
- Grip seems to be outdated in crystalshards.org HOT 1
- JSON error bug HOT 5
- Better json format HOT 1
- Route with override would cause instantiating new instance of controller for each request
- Feature request: File upload/download HOT 4
- Index syntax HOT 3
- Do you accept contributions? HOT 1
- Clarify documentation about exception handling HOT 1
- Handle arbitrary exceptions HOT 3
- To handle CORS preflight OPTIONS requests HOT 3
- Wrong behavoir of serve_static on Windows HOT 2
- [Feature] Set routing for serving static files HOT 8
- SHA1 Alternatives HOT 1
- Error: execution of command failed with exit status 1 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grip.