new error classes

OBSWSConnectionError, OBSWSRequestError added

they subclass OBSWSError

readme updated with new error classes
This commit is contained in:
onyx-and-iris 2023-08-11 02:22:14 +01:00
parent 515fa565d4
commit 976c8f19a8
11 changed files with 73 additions and 55 deletions

View File

@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
obsws (0.4.1) obsws (0.5.0)
waitutil (~> 0.2.1) waitutil (~> 0.2.1)
websocket-driver (~> 0.7.5) websocket-driver (~> 0.7.5)

View File

@ -109,7 +109,13 @@ def on_scene_created(data):
### Errors ### 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) For a full list of status codes refer to [Codes](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requeststatus)

View File

@ -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/req"
require_relative "obsws/event" require_relative "obsws/event"
require_relative "obsws/version"
module OBSWS 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 end

View File

@ -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 module OBSWS
class Base class Base
include Logging include Logging

View File

@ -1,6 +1,3 @@
require "socket"
require "websocket/driver"
module OBSWS module OBSWS
module Driver module Driver
class Socket class Socket

View File

@ -1,6 +0,0 @@
module OBSWS
module Error
class OBSWSError < StandardError
end
end
end

View File

@ -1,9 +1,3 @@
require "json"
require_relative "util"
require_relative "mixin"
require_relative "logger"
module OBSWS module OBSWS
module Events module Events
module SUBS module SUBS
@ -78,6 +72,11 @@ module OBSWS
kwargs[:subs] ||= SUBS::LOW_VOLUME kwargs[:subs] ||= SUBS::LOW_VOLUME
@base_client = Base.new(**kwargs) @base_client = Base.new(**kwargs)
logger.info("#{self} successfully identified with server") 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) { @base_client.updater = ->(op_code, data) {
if op_code == Mixin::OPCodes::EVENT if op_code == Mixin::OPCodes::EVENT
logger.debug("received: #{data}") logger.debug("received: #{data}")

View File

@ -1,5 +1,3 @@
require "logger"
module OBSWS module OBSWS
module Logging module Logging
def logger def logger

View File

@ -1,5 +1,3 @@
require_relative "util"
module OBSWS module OBSWS
module Mixin module Mixin
module Meta module Meta

View File

@ -1,22 +1,18 @@
require "waitutil"
require_relative "base"
require_relative "error"
require_relative "util"
require_relative "mixin"
require_relative "logger"
module OBSWS module OBSWS
module Requests module Requests
class Client class Client
include Logging include Logging
include Error
include Mixin::TearDown include Mixin::TearDown
include Mixin::OPCodes include Mixin::OPCodes
def initialize(**kwargs) def initialize(**kwargs)
@base_client = Base.new(**kwargs) @base_client = Base.new(**kwargs)
logger.info("#{self} successfully identified with server") 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) { @base_client.updater = ->(op_code, data) {
logger.debug("response received: #{data}") logger.debug("response received: #{data}")
@response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE @response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE
@ -48,19 +44,23 @@ module OBSWS
timeout_sec: 3 timeout_sec: 3
) { @response[:requestId] == id } ) { @response[:requestId] == id }
unless @response[:requestStatus][:result] unless @response[:requestStatus][:result]
error = [ raise OBSWSRequestError.new(@response[:requestType], @response[:requestStatus][:code], @response[:requestStatus][:comment])
"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"))
end end
@response[:responseData] @response[:responseData]
rescue WaitUtil::TimeoutError rescue OBSWSRequestError => e
msg = "no response with matching id received" err_msg = [
logger.error(msg) "#{e.class.name}: #{e.message}",
raise OBSWSError.new(msg) *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 end
def get_version def get_version

View File

@ -1,5 +1,5 @@
module OBSWS module OBSWS
module Version module VERSION
module_function module_function
def major def major
@ -7,11 +7,11 @@ module OBSWS
end end
def minor def minor
4 5
end end
def patch def patch
2 0
end end
def to_a def to_a
@ -22,6 +22,4 @@ module OBSWS
to_a.join(".") to_a.join(".")
end end
end end
VERSION = Version.to_s
end end