Skip to content

Commit

Permalink
Merge pull request #120 from jmoriau/set-error-and-logger-middlewares…
Browse files Browse the repository at this point in the history
…-position

Set positions for errors and logger middleware, added 2 views
  • Loading branch information
sdogruyol committed Mar 3, 2016
2 parents c2f0681 + 04ccb97 commit c7d2b33
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/kemal/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,21 @@ module Kemal
def setup_logging
if @logging
@logger ||= Kemal::CommonLogHandler.new(@env)
HANDLERS << @logger.not_nil!
HANDLERS.insert(0, @logger.not_nil!)
else
@logger = Kemal::NullLogHandler.new(@env)
HANDLERS << @logger.not_nil!
end
end

private def setup_error_handler
if @always_rescue
@error_handler ||= Kemal::CommonErrorHandler::INSTANCE
HANDLERS << @error_handler.not_nil!
HANDLERS.insert(1, @error_handler.not_nil!)
end
end

private def setup_public_folder
HANDLERS << Kemal::StaticFileHandler.new(@public_folder) if @serve_static
HANDLERS.insert(2, Kemal::StaticFileHandler.new(@public_folder)) if @serve_static
end
end

Expand Down
48 changes: 48 additions & 0 deletions src/kemal/view.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,51 @@ def render_500(context, ex)
context.response.print template
context
end

# Template for 415 Unsupported media type
def render_415(context, message)
template = <<-HTML
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body { text-align:center;font-family:helvetica,arial;font-size:22px;
color:#888;margin:20px}
#c {margin:0 auto;width:500px;text-align:left}
</style>
</head>
<body>
<h2>Unsupported media type</h2>
<h3>#{message}</h3>
<img src="/__kemal__/404.png">
</body>
</html>
HTML
context.response.status_code = 415
context.response.print template
context
end

# Template for 400 Bad request
def render_400(context, message)
template = <<-HTML
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body { text-align:center;font-family:helvetica,arial;font-size:22px;
color:#888;margin:20px}
#c {margin:0 auto;width:500px;text-align:left}
</style>
</head>
<body>
<h2>Bad request</h2>
<h3>#{message}</h3>
<img src="/__kemal__/404.png">
</body>
</html>
HTML
context.response.status_code = 400
context.response.print template
context
end

0 comments on commit c7d2b33

Please sign in to comment.