Skip to content

Commit

Permalink
Added error class per code review
Browse files Browse the repository at this point in the history
  • Loading branch information
val99erie committed Apr 11, 2017
1 parent bda6210 commit 37c4991
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
21 changes: 9 additions & 12 deletions lib/hydra/derivatives/processors/active_encode.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
require 'active_encode'

module Hydra::Derivatives::Processors
class ActiveEncodeError < StandardError
def initialize(status, source_path, errors = [])
msg = "ActiveEncode status was \"#{status}\" for #{source_path}"
msg = "#{msg}: #{errors.join(' ; ')}" unless errors.empty?
super(msg)
end
end

class ActiveEncode < Processor
def process
encode = ::ActiveEncode::Base.create(source_path, directives)

# Wait until the encoding job is finished
# while(encode.reload.running?) { sleep 10 }

raise_exception_if_encoding_failed(encode)
raise_exception_if_encoding_cancelled(encode)
raise ActiveEncodeError.new(encode.state, source_path, encode.errors) unless encode.completed?

# TODO: call output_file_service with the output url
end

def raise_exception_if_encoding_failed(encode)
return unless encode.failed?
raise StandardError.new("Encoding failed for #{source_path}: #{encode.errors.join(' ; ')}")
end

def raise_exception_if_encoding_cancelled(encode)
return unless encode.cancelled?
raise StandardError.new("Encoding cancelled for #{source_path}")
end
end
end
6 changes: 4 additions & 2 deletions spec/processors/active_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
# encode finished and returned a certain status.
let(:failed_status) { false }
let(:cancelled_status) { false }
let(:completed_status) { false }
let(:errors) { [] }
let(:encode_double) do
double('encode double',
reload: self, state: state, errors: errors,
:completed? => completed_status,
:failed? => failed_status,
:cancelled? => cancelled_status)
end
Expand All @@ -33,7 +35,7 @@
end

it 'raises an exception' do
expect { subject }.to raise_error("Encoding failed for #{file_path}: error 1 ; error 2")
expect { subject }.to raise_error(Hydra::Derivatives::Processors::ActiveEncodeError, "ActiveEncode status was \"failed\" for #{file_path}: error 1 ; error 2")
end
end

Expand All @@ -46,7 +48,7 @@
end

it 'raises an exception' do
expect { subject }.to raise_error("Encoding cancelled for #{file_path}")
expect { subject }.to raise_error(Hydra::Derivatives::Processors::ActiveEncodeError, "ActiveEncode status was \"cancelled\" for #{file_path}")
end
end
end
Expand Down

0 comments on commit 37c4991

Please sign in to comment.