subutux / json-rpc2php

A json-rpc 2.0 server for php and some clients for php,js,python and vala.
GNU General Public License v2.0
46 stars 26 forks source link

Custom error codes #5

Closed serge012374 closed 11 years ago

serge012374 commented 11 years ago

Hi, Subutux!

Is there a way to generate and use custom error codes?

Thank you!

Sergey

serge012374 commented 11 years ago

It looks like when I try to do something like this in my class:

throw new Exception( 'Custom Exception' ); throw new Exception( 'Custom Exception', -1 );

What I get back is: 500 Internal Server Error

I'd really like to get -1 Custom Exception. Is it possible?

Thank you so much!

Sergey

subutux commented 11 years ago

I'll look in to this.

subutux commented 11 years ago

Ok. It's been a while since I've used my library. It's possible to throw a custom exception using the JsonRPCException() handler. Use it as a normal Exception(), and you'll be good to go!

For ex:

if ($i = false) {
   throw new JsonRPCException("my custom exception message");
}

But, custom error codes (like your -1) will be set to the 'Internal Server Error' code (-32603).

If you really want to use '-1', you'll have to define it in the jsonRPC2Server.php file in these 3 arrays: $errorMessages, $errorMessagesFull and $errorCodes.

Please note that as of the Json-rpc 2.0 specification, errorcodes like -1 are not accepted. for custom error codes, you'll have to use an error code between -32000 and -32099. see http://www.jsonrpc.org/specification#error_object for more info.