From 077a14fe62f45620f04d6c9bc3fe0db2041abb98 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sat, 15 Jul 2023 00:16:35 +0100 Subject: [PATCH] VMCAPIError now takes fn and res as args --- lib/voicemeeter/cbindings.rb | 6 +++--- lib/voicemeeter/errors.rb | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/voicemeeter/cbindings.rb b/lib/voicemeeter/cbindings.rb index eddf1c7..ca4a9c8 100644 --- a/lib/voicemeeter/cbindings.rb +++ b/lib/voicemeeter/cbindings.rb @@ -56,11 +56,11 @@ module Voicemeeter res = send(fn, *args) if exp.nil? unless ok.include?(res) - raise Errors::VMCAPIError.new("#{fn} returned #{res}") + raise Errors::VMCAPIError.new(fn, res) end else - unless exp.call(res) && ok.include?(res) - raise Errors::VMCAPIError.new("#{fn} returned #{res}") + unless exp.call(res) || ok.include?(res) + raise Errors::VMCAPIError.new(fn, res) end end res diff --git a/lib/voicemeeter/errors.rb b/lib/voicemeeter/errors.rb index 4c82bdd..61ce1d8 100644 --- a/lib/voicemeeter/errors.rb +++ b/lib/voicemeeter/errors.rb @@ -1,9 +1,26 @@ +require_relative "core_extensions/string/camelcase" + module Voicemeeter module Errors class VMError < StandardError end class VMCAPIError < VMError + attr_reader :code + + def initialize(ruby_name, code) + @ruby_name = ruby_name + @code = code + super(message) + end + + def message + "#{fn_name} returned #{code}" + end + + def fn_name + "VBVMR_#{@ruby_name.to_s.delete_prefix("bind_").camelcase.sub("macro_button", "macrobutton")}" + end end end end