mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Improve common_exception_handler to handler custom 500 errors. Fixes #175.
This commit is contained in:
		
							parent
							
								
									c718b02dbc
								
							
						
					
					
						commit
						19581d6e3e
					
				
					 2 changed files with 37 additions and 21 deletions
				
			
		|  | @ -1,16 +1,22 @@ | ||||||
| require "./spec_helper" | require "./spec_helper" | ||||||
| 
 | 
 | ||||||
| describe "Kemal::CommonExceptionHandler" do | describe "Kemal::CommonExceptionHandler" do | ||||||
|   # it "renders 404 on route not found" do |   it "renders 404 on route not found" do | ||||||
|   #   get "/" do |env| |     get "/" do |env| | ||||||
|   #     "Hello" |       "Hello" | ||||||
|   #   end |     end | ||||||
|   # | 
 | ||||||
|   #   request = HTTP::Request.new("GET", "/asd") |     request = HTTP::Request.new("GET", "/asd") | ||||||
|   #   client_response = call_request_on_app(request) |     io = MemoryIO.new | ||||||
|   #   client_response.status_code.should eq 404 |     response = HTTP::Server::Response.new(io) | ||||||
|   # end |     context = HTTP::Server::Context.new(request, response) | ||||||
|   # |     Kemal::CommonExceptionHandler::INSTANCE.call(context) | ||||||
|  |     response.close | ||||||
|  |     io.rewind | ||||||
|  |     response = HTTP::Client::Response.from_io(io, decompress: false) | ||||||
|  |     response.status_code.should eq 404 | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|   # it "renders custom error" do |   # it "renders custom error" do | ||||||
|   #   error 403 do |   #   error 403 do | ||||||
|   #     "403 error" |   #     "403 error" | ||||||
|  | @ -21,8 +27,15 @@ describe "Kemal::CommonExceptionHandler" do | ||||||
|   #   end |   #   end | ||||||
|   # |   # | ||||||
|   #   request = HTTP::Request.new("GET", "/") |   #   request = HTTP::Request.new("GET", "/") | ||||||
|   #   client_response = call_request_on_app(request) |   #   io = MemoryIO.new | ||||||
|   #   client_response.status_code.should eq 403 |   #   response = HTTP::Server::Response.new(io) | ||||||
|   #   client_response.body.should eq "403 error" |   #   context = HTTP::Server::Context.new(request, response) | ||||||
|  |   #   Kemal::RouteHandler::INSTANCE.call(context) | ||||||
|  |   #   Kemal::CommonExceptionHandler::INSTANCE.call(context) | ||||||
|  |   #   response.close | ||||||
|  |   #   io.rewind | ||||||
|  |   #   response = HTTP::Client::Response.from_io(io, decompress: false) | ||||||
|  |   #   response.status_code.should eq 403 | ||||||
|  |   #   response.body.should eq "403 error" | ||||||
|   # end |   # end | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -7,21 +7,24 @@ module Kemal | ||||||
|         call_next(context) |         call_next(context) | ||||||
|       rescue Kemal::Exceptions::RouteNotFound |       rescue Kemal::Exceptions::RouteNotFound | ||||||
|         context.response.content_type = "text/html" |         context.response.content_type = "text/html" | ||||||
|         context.response.status_code = 404 |         call_exception_with_status_code(context, 404) | ||||||
|         context.response.print Kemal.config.error_handlers[404].call(context) |  | ||||||
|         return context |  | ||||||
|       rescue Kemal::Exceptions::CustomException |       rescue Kemal::Exceptions::CustomException | ||||||
|         status_code = context.response.status_code |         call_exception_with_status_code(context, context.response.status_code) | ||||||
|         if Kemal.config.error_handlers.has_key?(status_code) |  | ||||||
|           context.response.print Kemal.config.error_handlers[status_code].call(context) |  | ||||||
|           return context |  | ||||||
|         end |  | ||||||
|       rescue ex : Exception |       rescue ex : Exception | ||||||
|         context.response.content_type = "text/html" |         context.response.content_type = "text/html" | ||||||
|         Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace}\n") |         Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace}\n") | ||||||
|  |         return call_exception_with_status_code(context, 500) if Kemal.config.error_handlers.has_key?(500) | ||||||
|         verbosity = Kemal.config.env == "production" ? false : true |         verbosity = Kemal.config.env == "production" ? false : true | ||||||
|         return render_500(context, ex.inspect_with_backtrace, verbosity) |         return render_500(context, ex.inspect_with_backtrace, verbosity) | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|  | 
 | ||||||
|  |     def call_exception_with_status_code(context, status_code) | ||||||
|  |       if Kemal.config.error_handlers.has_key?(status_code) | ||||||
|  |         context.response.status_code = status_code | ||||||
|  |         context.response.print Kemal.config.error_handlers[status_code].call(context) | ||||||
|  |         return context | ||||||
|  |       end | ||||||
|  |     end | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue