]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 547126: Make the JSON-RPC interface return error codes that are numerically
authorMax Kanat-Alexander <mkanat@bugzilla.org>
Tue, 23 Feb 2010 00:11:49 +0000 (16:11 -0800)
committerMax Kanat-Alexander <mkanat@bugzilla.org>
Tue, 23 Feb 2010 00:11:49 +0000 (16:11 -0800)
identical to the XML-RPC error codes.
r=dkl, a=mkanat

Bugzilla/WebService/Server/JSONRPC.pm

index d6af25043159fa7d6f0a9480cb83ef8440e7099a..cc3e8058ecfc85a2a4d28997be6542b220d6b608 100644 (file)
@@ -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<error> 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<Bugzilla::WebService/ERRORS>.
+Errors with a numeric C<code> higher than 100000 are errors thrown by
+the JSON-RPC library that Bugzilla uses, not by Bugzilla.
 
 =head1 SEE ALSO