GithubHelp home page GithubHelp logo

Comments (1)

grkek avatar grkek commented on June 16, 2024 2

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.