--f46d0442816a2d0d4804b8c6cc05
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Feb 10, 2012 at 10:14 AM, tan yeun <tayetemp / gmail.com> wrote:

> Hi friends,
>
> I am developing a ruby software that depends on api from the other team
>
> Is there any good way to print the error message indicating it is
> generated from their api error?
>
> For example, there's a method provided from the api called foo()
>
> so when I do
> api.foo()
> it will return an error message : "foo error"
>
> when I develop my code, I want the error message to look like :
> "api: foo error"
>
> that way, when I see this error message, then I know it's the api error,
> not my code's error.
>
>
If your code is using the API wrong, then it is an error with your code.

If they're changing the API, then I suppose you should ask to be kept in
communication (if you're consuming their code, then you're their customer,
your needs should drive such changes).

If you're just thinking their code is buggy and wrong, then IDK. I want to
say "just go fix it", but maybe your situation is different than mine. At
the very least, you can get very far without actually depending on their
code. In your tests, use dependency injection to inject a mock version of
their code that has the same API but will do whatever you are telling it to
do. Then you can build your code all against your mocks, and hook up to
their real code in production. It would be nice if the mock class could
have a way of asserting that the real object matches its public interface.



> So far the best practice I can think of is to wrap around all the
> methods
> provided by the api, for example
>
> class apiWrap
>  def initialize(api)
>    @api  pi
>  end
>  def foo
>    begin
>      @api.foo()
>    rescue e
>      raise "api: #{e.message}"
>    end
>  end
> end
>
>
So, two things.
1) you can rescue a method, so the begin/end are unnecessary:
def foo
  @api.foo
rescue e
  raise "..."
end

2) Rather than adding this to every method you are dealing with, how about
just checking exceptions that kill the app, and printing something out to
indicate to yourself that this error came from their API (my understanding
is that you're having difficulty telling where errors are coming from and
just want some kind of indicator)

at_exit do
  if $! && $!.backtrace.any? { |line| line /their project name/ }
    $stderr.puts "ERROR INTERFACING WITH OTHER TEAM'S API"
  end
end

--f46d0442816a2d0d4804b8c6cc05--