Skip to content

Commit

Permalink
Add more exceptions just to be a bit more user friendly, update the v…
Browse files Browse the repository at this point in the history
…ersion in the shard, remove dead code from dsl module
  • Loading branch information
grkek committed Oct 30, 2022
1 parent 237cfe8 commit 942546e
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 53 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ This project exists due to the fact that Kemal lacks one crucial part of a frame
- WebSocket RFC 6455 support.
- Built-in exceptions support.
- Parameter handling support.
- JSON serialization and deserialization support (fastest framework with JSON).
- Built-in third-party module support.
- JSON serialization and deserialization support (fastest framework with JSON in Crystal).
- Middleware support.
- Request/Response context, inspired by [expressjs](https://github.com/expressjs/express).
- Advanced routing support.

Expand Down Expand Up @@ -123,13 +123,8 @@ class Application < Grip::Application
exceptions [Grip::Exceptions::Unauthorized, Grip::Exceptions::NotFound], ExceptionController
exception ArgumentError, ExceptionController

pipeline :api, [
Authorization.new
]

pipeline :docs, [
PoweredByGrip.new
]
pipeline :api, [PoweredByGrip.new, Authorization.new]
pipeline :docs, [PoweredByGrip.new]

scope "/api" do
get "/error", IndexController, as: :error
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: grip
version: 0.3.0
version: 2.0.0

authors:
- Grip and its Contributors <https://github.com/grip-framework/>
Expand Down
6 changes: 1 addition & 5 deletions src/grip/exceptions/bad_request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ module Grip
module Exceptions
class BadRequest < Base
def initialize
@status = HTTP::Status::BAD_REQUEST
@status_code = HTTP::Status::BAD_REQUEST
super "Please provide a proper request to the endpoint."
end

def status_code : Int32
@status.not_nil!.value
end
end
end
end
6 changes: 1 addition & 5 deletions src/grip/exceptions/base.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
module Grip
module Exceptions
abstract class Base < Exception
getter status : HTTP::Status?

def status_code : Int32
@status.not_nil!.value
end
getter status_code : HTTP::Status = HTTP::Status::IM_A_TEAPOT
end
end
end
6 changes: 1 addition & 5 deletions src/grip/exceptions/forbidden.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ module Grip
module Exceptions
class Forbidden < Base
def initialize
@status = HTTP::Status::FORBIDDEN
@status_code = HTTP::Status::FORBIDDEN
super "You lack the privilege to access this endpoint."
end

def status_code : Int32
@status.not_nil!.value
end
end
end
end
5 changes: 1 addition & 4 deletions src/grip/exceptions/generic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ module Grip
module Exceptions
class Generic < Base
def initialize(@status : HTTP::Status)
@status_code = HTTP::Status::SERVICE_UNAVAILABLE
super "Something went wrong, please try again."
end

def status_code : Int32
@status.not_nil!.value
end
end
end
end
6 changes: 1 addition & 5 deletions src/grip/exceptions/internal_server_error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ module Grip
module Exceptions
class InternalServerError < Base
def initialize
@status = HTTP::Status::INTERNAL_SERVER_ERROR
@status_code = HTTP::Status::INTERNAL_SERVER_ERROR
super "Please try again later or contact the server administration team."
end

def status_code : Int32
@status.not_nil!.value
end
end
end
end
6 changes: 1 addition & 5 deletions src/grip/exceptions/method_not_allowed.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ module Grip
module Exceptions
class MethodNotAllowed < Base
def initialize
@status = HTTP::Status::METHOD_NOT_ALLOWED
@status_code = HTTP::Status::METHOD_NOT_ALLOWED
super "Please provide a proper request to the endpoint."
end

def status_code : Int32
@status.not_nil!.value
end
end
end
end
10 changes: 10 additions & 0 deletions src/grip/exceptions/not_acceptable.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Grip
module Exceptions
class NotAcceptable < Base
def initialize
@status_code = HTTP::Status::NOT_ACCEPTABLE
super "Please provide a proper request to the endpoint."
end
end
end
end
6 changes: 1 addition & 5 deletions src/grip/exceptions/not_found.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ module Grip
module Exceptions
class NotFound < Base
def initialize
@status = HTTP::Status::NOT_FOUND
@status_code = HTTP::Status::NOT_FOUND
super "The endpoint you have requested was not found on the server."
end

def status_code : Int32
@status.not_nil!.value
end
end
end
end
10 changes: 10 additions & 0 deletions src/grip/exceptions/payload_too_large.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Grip
module Exceptions
class PayloadTooLarge < Base
def initialize
@status_code = HTTP::Status::PAYLOAD_TOO_LARGE
super "Your request to the endpoint has been denied, please provide a proper payload."
end
end
end
end
10 changes: 10 additions & 0 deletions src/grip/exceptions/request_timeout.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Grip
module Exceptions
class RequestTimeout < Base
def initialize
@status_code = HTTP::Status::REQUEST_TIMEOUT
super "Your request to the endpoint has timed out."
end
end
end
end
10 changes: 10 additions & 0 deletions src/grip/exceptions/too_many_requests.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Grip
module Exceptions
class TooManyRequests < Base
def initialize
@status_code = HTTP::Status::TOO_MANY_REQUESTS
super "Your request to the endpoint has been limited, please try again later."
end
end
end
end
6 changes: 1 addition & 5 deletions src/grip/exceptions/unauthorized.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ module Grip
module Exceptions
class Unauthorized < Base
def initialize
@status = HTTP::Status::UNAUTHORIZED
@status_code = HTTP::Status::UNAUTHORIZED
super "You are not authorized to access this endpoint."
end

def status_code : Int32
@status.not_nil!.value
end
end
end
end
4 changes: 2 additions & 2 deletions src/grip/handlers/exception.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module Grip
context.response.status_code = 500 if !context.response.status_code.in?([400, 401, 403, 404, 405, 500])

if ex.is_a?(Grip::Exceptions::Base)
context.response.status_code = ex.status_code
call_exception(context, ex, ex.status_code)
context.response.status_code = ex.status_code.value
call_exception(context, ex, ex.status_code.value)
else
call_exception(context, ex, context.response.status_code)
end
Expand Down
2 changes: 0 additions & 2 deletions src/grip/macros/dsl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ module Grip
case {{valve}}
when Symbol
@valves.push({{valve}})
else
# Ignore this condition
end
end

Expand Down

0 comments on commit 942546e

Please sign in to comment.