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
remote: .
specs:
obsws (0.4.1)
obsws (0.5.0)
waitutil (~> 0.2.1)
websocket-driver (~> 0.7.5)

View File

@ -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)

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/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

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

View File

@ -1,6 +1,3 @@
require "socket"
require "websocket/driver"
module OBSWS
module Driver
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 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}")

View File

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

View File

@ -1,5 +1,3 @@
require_relative "util"
module OBSWS
module Mixin
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 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

View File

@ -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