diff --git a/Gemfile.lock b/Gemfile.lock index 199be40..7a6eb87 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - obsws (0.4.1) + obsws (0.5.0) waitutil (~> 0.2.1) websocket-driver (~> 0.7.5) diff --git a/README.md b/README.md index 26fae94..ac9063c 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,13 @@ def on_scene_created(data): ### Errors -If a request fails an `OBSWSError` will be raised with a status code. +If a general error occurs an `OBSWSError` will be raised. + +If a connection attempt fails or times out an `OBSWSConnectionError` will be raised. + +If a request fails an `OBSWSRequestError` will be raised with a status code. + +- The request name and code are retrievable through attributes {OBSWSRequestError}.name and {OBSWSRequestError}.code For a full list of status codes refer to [Codes](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requeststatus) diff --git a/lib/obsws.rb b/lib/obsws.rb index d2bb201..e8d9143 100644 --- a/lib/obsws.rb +++ b/lib/obsws.rb @@ -1,5 +1,42 @@ +require "digest/sha2" +require "json" +require "waitutil" +require "socket" +require "websocket/driver" +require "logger" + +require_relative "obsws/logger" +require_relative "obsws/driver" +require_relative "obsws/util" +require_relative "obsws/mixin" +require_relative "obsws/base" + require_relative "obsws/req" require_relative "obsws/event" +require_relative "obsws/version" + module OBSWS + class OBSWSError < StandardError; end + + class OBSWSConnectionError < OBSWSError; end + + class OBSWSRequestError < OBSWSError + attr_reader :name, :code + + def initialize(name, code, msg) + @name = name + @code = code + @msg = msg + super(message) + end + + def message + msg = [ + "Request #{@name} returned code #{@code}." + ] + msg << "With message: #{@msg}" if @msg + msg.join(" ") + end + end end diff --git a/lib/obsws/base.rb b/lib/obsws/base.rb index 5696efd..ad4d9bb 100644 --- a/lib/obsws/base.rb +++ b/lib/obsws/base.rb @@ -1,12 +1,3 @@ -require "digest/sha2" -require "json" -require "waitutil" - -require_relative "driver" -require_relative "error" -require_relative "logger" -require_relative "mixin" - module OBSWS class Base include Logging diff --git a/lib/obsws/driver.rb b/lib/obsws/driver.rb index d38a291..ba6fcad 100644 --- a/lib/obsws/driver.rb +++ b/lib/obsws/driver.rb @@ -1,6 +1,3 @@ -require "socket" -require "websocket/driver" - module OBSWS module Driver class Socket diff --git a/lib/obsws/error.rb b/lib/obsws/error.rb deleted file mode 100644 index 25cc8e9..0000000 --- a/lib/obsws/error.rb +++ /dev/null @@ -1,6 +0,0 @@ -module OBSWS - module Error - class OBSWSError < StandardError - end - end -end diff --git a/lib/obsws/event.rb b/lib/obsws/event.rb index ff6ec3c..3f641e1 100644 --- a/lib/obsws/event.rb +++ b/lib/obsws/event.rb @@ -1,9 +1,3 @@ -require "json" - -require_relative "util" -require_relative "mixin" -require_relative "logger" - module OBSWS module Events module SUBS @@ -78,6 +72,11 @@ module OBSWS kwargs[:subs] ||= SUBS::LOW_VOLUME @base_client = Base.new(**kwargs) logger.info("#{self} successfully identified with server") + rescue Errno::ECONNREFUSED, WaitUtil::TimeoutError => e + msg = "#{e.class.name}: #{e.message}" + logger.error(msg) + raise OBSWSConnectionError.new(msg) + else @base_client.updater = ->(op_code, data) { if op_code == Mixin::OPCodes::EVENT logger.debug("received: #{data}") diff --git a/lib/obsws/logger.rb b/lib/obsws/logger.rb index 8ac04eb..6cecd9b 100644 --- a/lib/obsws/logger.rb +++ b/lib/obsws/logger.rb @@ -1,5 +1,3 @@ -require "logger" - module OBSWS module Logging def logger diff --git a/lib/obsws/mixin.rb b/lib/obsws/mixin.rb index 6718d6e..70f43c0 100644 --- a/lib/obsws/mixin.rb +++ b/lib/obsws/mixin.rb @@ -1,5 +1,3 @@ -require_relative "util" - module OBSWS module Mixin module Meta diff --git a/lib/obsws/req.rb b/lib/obsws/req.rb index ca916bd..daf311e 100644 --- a/lib/obsws/req.rb +++ b/lib/obsws/req.rb @@ -1,22 +1,18 @@ -require "waitutil" - -require_relative "base" -require_relative "error" -require_relative "util" -require_relative "mixin" -require_relative "logger" - module OBSWS module Requests class Client include Logging - include Error include Mixin::TearDown include Mixin::OPCodes def initialize(**kwargs) @base_client = Base.new(**kwargs) logger.info("#{self} successfully identified with server") + rescue Errno::ECONNREFUSED, WaitUtil::TimeoutError => e + msg = "#{e.class.name}: #{e.message}" + logger.error(msg) + raise OBSWSConnectionError.new(msg) + else @base_client.updater = ->(op_code, data) { logger.debug("response received: #{data}") @response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE @@ -48,19 +44,23 @@ module OBSWS timeout_sec: 3 ) { @response[:requestId] == id } unless @response[:requestStatus][:result] - error = [ - "Request #{@response[:requestType]} returned code #{@response[:requestStatus][:code]}" - ] - if @response[:requestStatus].key?(:comment) - error << ["With message: #{@response[:requestStatus][:comment]}"] - end - raise OBSWSError.new(error.join("\n")) + raise OBSWSRequestError.new(@response[:requestType], @response[:requestStatus][:code], @response[:requestStatus][:comment]) end @response[:responseData] - rescue WaitUtil::TimeoutError - msg = "no response with matching id received" - logger.error(msg) - raise OBSWSError.new(msg) + rescue OBSWSRequestError => e + err_msg = [ + "#{e.class.name}: #{e.message}", + *e.backtrace + ] + logger.error(err_msg.join("\n")) + raise + rescue WaitUtil::TimeoutError => e + err_msg = [ + "#{e.class.name}: #{e.message}", + *e.backtrace + ] + logger.error(err_msg) + raise OBSWSError.new(err_msg) end def get_version diff --git a/lib/obsws/version.rb b/lib/obsws/version.rb index fcb9642..80dae86 100644 --- a/lib/obsws/version.rb +++ b/lib/obsws/version.rb @@ -1,5 +1,5 @@ module OBSWS - module Version + module VERSION module_function def major @@ -7,11 +7,11 @@ module OBSWS end def minor - 4 + 5 end def patch - 2 + 0 end def to_a @@ -22,6 +22,4 @@ module OBSWS to_a.join(".") end end - - VERSION = Version.to_s end