-
-
Save paulcsmith/93bbc384be35a39c91f796d4106c9935 to your computer and use it in GitHub Desktop.
Helpful compile time errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Response | |
end | |
abstract class BaseAction | |
abstract def call : Response | |
def redirect(location : String, permanent : Bool = false) | |
# do stuff | |
end | |
compile_error redirect(_location, status : Int32) | |
<<-ERROR | |
The status defaults to 301, but you can pass `permanent: true` for a 302. | |
ERROR | |
end | |
# No catch all is defined so it just uses the default compile error if no `redirect` method matches, | |
# and the status is not an Int32 | |
# If one of the return types is Nil | |
compile_error call : Nil | |
<<-ERROR | |
You must always return a Response from call, but it currently returns a `nil` value. This often happens because you | |
have a branch that doesn not have an `else`, or you forgot to call `render` or `redirect` | |
ERROR | |
end | |
# Define a custom error if `call` returns something other than | |
# Response or Nil (because Nil would be caught by the more specific compile_error) | |
compile_error call | |
<<-ERROR | |
You must always return a Response from call. Remember to call `render` or `redirect` at the end of your action. | |
ERROR | |
end | |
end | |
class Users::Index < BaseAction | |
def call | |
redirect("/somewhere", 302) | |
if some_condition? | |
Response.new | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment