mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Set content-type to text/html on errors.Fixes #130
This commit is contained in:
		
							parent
							
								
									5d1c4859d7
								
							
						
					
					
						commit
						664673f125
					
				
					 5 changed files with 20 additions and 23 deletions
				
			
		
							
								
								
									
										11
									
								
								spec/common_exception_handler_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								spec/common_exception_handler_spec.cr
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,11 @@ | ||||||
|  | require "./spec_helper" | ||||||
|  | 
 | ||||||
|  | describe "Kemal::CommonExceptionHandler" do | ||||||
|  |   it "renders 404 on route not found" do | ||||||
|  |     common_exception_handler = Kemal::CommonExceptionHandler::INSTANCE | ||||||
|  |     request = HTTP::Request.new("GET", "/?message=world") | ||||||
|  |     io_with_context = create_request_and_return_io(common_exception_handler, request) | ||||||
|  |     client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) | ||||||
|  |     client_response.status_code.should eq 404 | ||||||
|  |   end | ||||||
|  | end | ||||||
|  | @ -108,27 +108,6 @@ describe "Kemal::RouteHandler" do | ||||||
|     client_response.body.should eq("Skills ruby,crystal") |     client_response.body.should eq("Skills ruby,crystal") | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   # Removed until there is a way to test multiple middlewares |  | ||||||
|   # it "renders 404 on not found" do |  | ||||||
|   #  kemal = Kemal::RouteHandler::INSTANCE |  | ||||||
|   #  request = HTTP::Request.new("GET", "/?message=world") |  | ||||||
|   #  io_with_context = create_request_and_return_io(kemal, request) |  | ||||||
|   #  client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) |  | ||||||
|   #  client_response.status_code.should eq 404 |  | ||||||
|   # end |  | ||||||
| 
 |  | ||||||
|   # it "renders 500 on exception" do |  | ||||||
|   #   kemal = Kemal::RouteHandler::INSTANCE |  | ||||||
|   #   kemal.add_route "GET", "/" do |  | ||||||
|   #     raise "Exception" |  | ||||||
|   #   end |  | ||||||
|   #   request = HTTP::Request.new("GET", "/?message=world") |  | ||||||
|   #   io_with_context = create_request_and_return_io(kemal, request) |  | ||||||
|   #   client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) |  | ||||||
|   #   client_response.status_code.should eq 500 |  | ||||||
|   #   client_response.body.includes?("Exception").should eq true |  | ||||||
|   # end |  | ||||||
|   # |  | ||||||
|   it "checks for _method param in POST request to simulate PUT" do |   it "checks for _method param in POST request to simulate PUT" do | ||||||
|     kemal = Kemal::RouteHandler::INSTANCE |     kemal = Kemal::RouteHandler::INSTANCE | ||||||
|     kemal.add_route "PUT", "/" do |env| |     kemal.add_route "PUT", "/" do |env| | ||||||
|  |  | ||||||
|  | @ -1,13 +1,15 @@ | ||||||
| class Kemal::CommonErrorHandler < HTTP::Handler | class Kemal::CommonExceptionHandler < HTTP::Handler | ||||||
|   INSTANCE = new |   INSTANCE = new | ||||||
| 
 | 
 | ||||||
|   def call(context) |   def call(context) | ||||||
|     begin |     begin | ||||||
|       call_next context |       call_next context | ||||||
|     rescue ex : Kemal::Exceptions::RouteNotFound |     rescue ex : Kemal::Exceptions::RouteNotFound | ||||||
|  |       context.response.content_type = "text/html" | ||||||
|       Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace.colorize(:red)}\n") |       Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace.colorize(:red)}\n") | ||||||
|       return render_404(context) |       return render_404(context) | ||||||
|     rescue ex |     rescue ex | ||||||
|  |       context.response.content_type = "text/html" | ||||||
|       Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace.colorize(:red)}\n") |       Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace.colorize(:red)}\n") | ||||||
|       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) | ||||||
|  | @ -57,7 +57,7 @@ module Kemal | ||||||
| 
 | 
 | ||||||
|     private def setup_error_handler |     private def setup_error_handler | ||||||
|       if @always_rescue |       if @always_rescue | ||||||
|         @error_handler ||= Kemal::CommonErrorHandler::INSTANCE |         @error_handler ||= Kemal::CommonExceptionHandler::INSTANCE | ||||||
|         HANDLERS.insert(1, @error_handler.not_nil!) |         HANDLERS.insert(1, @error_handler.not_nil!) | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|  |  | ||||||
|  | @ -17,6 +17,7 @@ def render_403(context) | ||||||
|       </body> |       </body> | ||||||
|       </html> |       </html> | ||||||
|   HTML |   HTML | ||||||
|  |   context.response.content_type = "text/html" | ||||||
|   context.response.status_code = 403 |   context.response.status_code = 403 | ||||||
|   context.response.print template |   context.response.print template | ||||||
|   context |   context | ||||||
|  | @ -40,6 +41,7 @@ def render_404(context) | ||||||
|       </body> |       </body> | ||||||
|       </html> |       </html> | ||||||
|   HTML |   HTML | ||||||
|  |   context.response.content_type = "text/html" | ||||||
|   context.response.status_code = 404 |   context.response.status_code = 404 | ||||||
|   context.response.print template |   context.response.print template | ||||||
|   context |   context | ||||||
|  | @ -72,6 +74,7 @@ def render_500(context, backtrace, verbosity) | ||||||
|       </body> |       </body> | ||||||
|       </html> |       </html> | ||||||
|   HTML |   HTML | ||||||
|  |   context.response.content_type = "text/html" | ||||||
|   context.response.status_code = 500 |   context.response.status_code = 500 | ||||||
|   context.response.print template |   context.response.print template | ||||||
|   context |   context | ||||||
|  | @ -96,6 +99,7 @@ def render_415(context, message) | ||||||
|           </body> |           </body> | ||||||
|           </html> |           </html> | ||||||
|       HTML |       HTML | ||||||
|  |   context.response.content_type = "text/html" | ||||||
|   context.response.status_code = 415 |   context.response.status_code = 415 | ||||||
|   context.response.print template |   context.response.print template | ||||||
|   context |   context | ||||||
|  | @ -120,6 +124,7 @@ def render_400(context, message) | ||||||
|           </body> |           </body> | ||||||
|           </html> |           </html> | ||||||
|       HTML |       HTML | ||||||
|  |   context.response.content_type = "text/html" | ||||||
|   context.response.status_code = 400 |   context.response.status_code = 400 | ||||||
|   context.response.print template |   context.response.print template | ||||||
|   context |   context | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue