From 2f34ebff96ec03af7aa06074f706b8a696f6cd36 Mon Sep 17 00:00:00 2001 From: Max Kanat-Alexander Date: Mon, 22 Feb 2010 16:11:00 -0800 Subject: Bug 547126: Make the JSON-RPC return error codes that are numerically identical to the XML-RPC error codes. r=dkl, a=mkanat --- Bugzilla/WebService/Server/JSONRPC.pm | 57 ++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 5 deletions(-) (limited to 'Bugzilla') diff --git a/Bugzilla/WebService/Server/JSONRPC.pm b/Bugzilla/WebService/Server/JSONRPC.pm index d6af25043..cc3e8058e 100644 --- a/Bugzilla/WebService/Server/JSONRPC.pm +++ b/Bugzilla/WebService/Server/JSONRPC.pm @@ -85,6 +85,43 @@ sub datetime_format { return $iso_datetime; } +# Make all error messages returned by JSON::RPC go into the 100000 +# range, and bring down all our errors into the normal range. +sub _error { + my ($self, $id, $code) = (shift, shift, shift); + # All JSON::RPC errors are less than 1000. + if ($code < 1000) { + $code += 100000; + } + # Bugzilla::Error adds 100,000 to all *our* errors, so + # we know they came from us. + elsif ($code > 100000) { + $code -= 100000; + } + + # We can't just set $_[1] because it's not always settable, + # in JSON::RPC::Server. + unshift(@_, $id, $code); + my $json = $self->SUPER::_error(@_); + + # We want to always send the JSON-RPC 1.1 error format, although + # If we're not in JSON-RPC 1.1, we don't need the silly "name" parameter. + if (!$self->version) { + my $object = $self->json->decode($json); + my $message = $object->{error}; + # Just assure that future versions of JSON::RPC don't change the + # JSON-RPC 1.0 error format. + if (!ref $message) { + $object->{error} = { + code => $code, + message => $message, + }; + $json = $self->json->encode($object); + } + } + return $json; +} + ################## # Login Handling # ################## @@ -254,12 +291,22 @@ All other types are standard JSON types. =head1 ERRORS -All errors thrown by Bugzilla itself have 100000 added to their numeric -code. So, if the documentation says that an error is C<302>, then -it will be C<100302> when it is thrown via JSON-RPC. +JSON-RPC 1.0 and JSON-RPC 1.1 both return an C element when they +throw an error. In Bugzilla, the error contents look like: + + { message: 'Some message here', code: 123 } + +So, for example, in JSON-RPC 1.0, an error response would look like: + + { + result: null, + error: { message: 'Some message here', code: 123 }, + id: 1 + } -Errors less than 100000 are errors thrown by the JSON-RPC library that -Bugzilla uses, not by Bugzilla. +Every error has a "code", as described in L. +Errors with a numeric C higher than 100000 are errors thrown by +the JSON-RPC library that Bugzilla uses, not by Bugzilla. =head1 SEE ALSO -- cgit v1.2.3-24-g4f1b