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" | ||||
| 
 | ||||
| describe "Kemal::CommonExceptionHandler" do | ||||
|   # it "renders 404 on route not found" do | ||||
|   #   get "/" do |env| | ||||
|   #     "Hello" | ||||
|   #   end | ||||
|   # | ||||
|   #   request = HTTP::Request.new("GET", "/asd") | ||||
|   #   client_response = call_request_on_app(request) | ||||
|   #   client_response.status_code.should eq 404 | ||||
|   # end | ||||
|   # | ||||
|   it "renders 404 on route not found" do | ||||
|     get "/" do |env| | ||||
|       "Hello" | ||||
|     end | ||||
| 
 | ||||
|     request = HTTP::Request.new("GET", "/asd") | ||||
|     io = MemoryIO.new | ||||
|     response = HTTP::Server::Response.new(io) | ||||
|     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 | ||||
|   #   error 403 do | ||||
|   #     "403 error" | ||||
|  | @ -21,8 +27,15 @@ describe "Kemal::CommonExceptionHandler" do | |||
|   #   end | ||||
|   # | ||||
|   #   request = HTTP::Request.new("GET", "/") | ||||
|   #   client_response = call_request_on_app(request) | ||||
|   #   client_response.status_code.should eq 403 | ||||
|   #   client_response.body.should eq "403 error" | ||||
|   #   io = MemoryIO.new | ||||
|   #   response = HTTP::Server::Response.new(io) | ||||
|   #   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 | ||||
|  |  | |||
|  | @ -7,21 +7,24 @@ module Kemal | |||
|         call_next(context) | ||||
|       rescue Kemal::Exceptions::RouteNotFound | ||||
|         context.response.content_type = "text/html" | ||||
|         context.response.status_code = 404 | ||||
|         context.response.print Kemal.config.error_handlers[404].call(context) | ||||
|         return context | ||||
|         call_exception_with_status_code(context, 404) | ||||
|       rescue Kemal::Exceptions::CustomException | ||||
|         status_code = 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 | ||||
|         call_exception_with_status_code(context, context.response.status_code) | ||||
|       rescue ex : Exception | ||||
|         context.response.content_type = "text/html" | ||||
|         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 | ||||
|         return render_500(context, ex.inspect_with_backtrace, verbosity) | ||||
|       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 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue