From a094f3bf771b27af31a9cd4ee9aab0750892f97e Mon Sep 17 00:00:00 2001 From: Pulkit Bhatia Date: Wed, 3 Aug 2016 15:26:39 +0530 Subject: [PATCH 1/2] Raising MultipartParseError with error_code on_error() --- lib/reel.rb | 7 +++++++ lib/reel/request/multipart.rb | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/reel.rb b/lib/reel.rb index ea0af18..acb95b0 100644 --- a/lib/reel.rb +++ b/lib/reel.rb @@ -42,4 +42,11 @@ class UnsupportedMethodError < ArgumentError; end # wrong state for a given operation class StateError < RuntimeError; end + # Multipart Paring Error + class MultipartParseError < RequestError + def initialize error_code + @error_code = error_code + end + attr_reader :error_code + end end diff --git a/lib/reel/request/multipart.rb b/lib/reel/request/multipart.rb index 3a459e3..e9ffb41 100644 --- a/lib/reel/request/multipart.rb +++ b/lib/reel/request/multipart.rb @@ -55,7 +55,7 @@ def initialize(body, boundary) } end - @reader.on_error{|msg| warn msg } + @reader.on_error{|msg| raise MultipartParseError.new(400), msg } end From 783dfd4610248ae94cfb2a1cc1effd1dd98e09cc Mon Sep 17 00:00:00 2001 From: Pulkit Bhatia Date: Thu, 4 Aug 2016 15:42:56 +0530 Subject: [PATCH 2/2] Minor refactoring on raising error and associated test --- lib/reel.rb | 3 ++- lib/reel/request/multipart.rb | 4 ++-- spec/reel/multipart_spec.rb | 10 ++++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/reel.rb b/lib/reel.rb index acb95b0..83626a5 100644 --- a/lib/reel.rb +++ b/lib/reel.rb @@ -42,7 +42,8 @@ class UnsupportedMethodError < ArgumentError; end # wrong state for a given operation class StateError < RuntimeError; end - # Multipart Paring Error + # Multipart Parsing Error + # :error_code paramter can be used for responding purpose class MultipartParseError < RequestError def initialize error_code @error_code = error_code diff --git a/lib/reel/request/multipart.rb b/lib/reel/request/multipart.rb index e9ffb41..e8baa8d 100644 --- a/lib/reel/request/multipart.rb +++ b/lib/reel/request/multipart.rb @@ -54,7 +54,7 @@ def initialize(body, boundary) #TODO : expose part information if needed } end - + # Raising MultipartParseError with error code 400 @reader.on_error{|msg| raise MultipartParseError.new(400), msg } end @@ -67,8 +67,8 @@ def decode begin @body.each { |chunks| write chunks } rescue => e - warn e @files = {} + raise e end @files end diff --git a/spec/reel/multipart_spec.rb b/spec/reel/multipart_spec.rb index c522649..3d4a40a 100644 --- a/spec/reel/multipart_spec.rb +++ b/spec/reel/multipart_spec.rb @@ -146,15 +146,17 @@ raise ex if ex end - it "Parsing error for wrong boundary value" do + it "MultipartParseError for wrong boundary value" do ex = nil handler = proc do |connection| begin req = connection.request - expect(req.multipart? req.body).to eq true - expect(req.multipart.empty?).to eq true - + begin + req.multipart + rescue => e + expect(e.is_a? Reel::MultipartParseError).to eq true + end req.respond :ok, response_body rescue => ex end