FTB-Gamepedia / MediaWiki-Butt-Ruby

A Ruby library for the MediaWiki API
https://rubygems.org/gems/mediawiki-butt
MIT License
9 stars 2 forks source link

Proper error handling #32

Closed elifoster closed 8 years ago

elifoster commented 8 years ago

Right now, if there's an error, we simply return the error code as a String. This makes it difficult or weird to check for errors. For example, we want to be able to do this, as is idiomatic in Ruby:

begin
  hash = butt.some_function
  return hash['important_value']
rescue SomeError
  p 'Oh no we errored'
end

but right now, we have to do this:

hash = butt.some_function
if hash.is_a?(String)
  p 'Oh no we errored'
else
  return hash['important_value']
end

or this, for methods that return a String either way

string = butt.other_function
if /Error:/ =~ string
  p 'Oh no we errored'
else
  return string.reverse!
end

This is awful.

elifoster commented 8 years ago

(See #42).

We have a couple options I've thought of:

Categorized errors with a description given in the constructor:

# We have determined that the edit has errored:
fail EditError.new(error_message)

Specifically defined errors

# We have determined that the edit has errored
case error_message
when 'badtoken'
  fail BadToken.new
# ...
end

The benefits to the first:

The benefits to the second:

@APerson241 @xbony2

enterprisey commented 8 years ago

(For the record, I strongly support the first one, for the reason you mentioned.)

enterprisey commented 8 years ago

@elifoster and @xbony2, what are your opinions on option 1 vs option 2?

xbony2 commented 8 years ago

Whatever work.

elifoster commented 8 years ago

I like option 1

enterprisey commented 8 years ago

I've implemented option 1 (for just edit()) in #42.

elifoster commented 8 years ago

I have finally finished this up.