]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 435507: Provide a method of hooking the WebService error codes
authormkanat%bugzilla.org <>
Wed, 28 May 2008 03:11:56 +0000 (03:11 +0000)
committermkanat%bugzilla.org <>
Wed, 28 May 2008 03:11:56 +0000 (03:11 +0000)
Patch By Max Kanat-Alexander <mkanat@bugzilla.org> r=ghendricks, a=mkanat

Bugzilla/Error.pm
Bugzilla/Hook.pm
extensions/example/code/webservice-error_codes.pl [new file with mode: 0644]
extensions/example/lib/WSExample.pm
extensions/example/template/en/global/user-error-errors.html.tmpl [new file with mode: 0644]

index 3e5688e2a49075c774d8b6441ab7800c9e491b29..d15336a81c751b3ac75a6e8e2e92124f42fbe32e 100644 (file)
@@ -102,7 +102,12 @@ sub _throw_error {
             die("$message\n");
         }
         elsif (Bugzilla->error_mode == ERROR_MODE_DIE_SOAP_FAULT) {
-            my $code = WS_ERROR_CODE->{$error};
+            # Clone the hash so we aren't modifying the constant.
+            my %error_map = %{ WS_ERROR_CODE() };
+            require Bugzilla::Hook;
+            Bugzilla::Hook::process('webservice-error_codes', 
+                                    { error_map => \%error_map });
+            my $code = $error_map{$error};
             if (!$code) {
                 $code = ERROR_UNKNOWN_FATAL if $name =~ /code/i;
                 $code = ERROR_UNKNOWN_TRANSIENT if $name =~ /user/i;
index 0cf566db10289271c60c6923077d8a19eaddabfc..820693ee026bd49c4a5ef8675d4a98ae28f88edc 100644 (file)
@@ -262,3 +262,22 @@ WebService functions (and so that you also don't conflict with other
 plugins).
 
 =back
+
+=head2 webservice-error_codes
+
+If your webservice extension throws custom errors, you can set numeric
+codes for those errors here.
+
+Extensions should use error codes above 10000, unless they are re-using
+an already-existing error code.
+
+Params:
+
+=over
+
+=item C<error_map>
+
+A hash that maps the names of errors (like C<invalid_param>) to numbers.
+See L<Bugzilla::WebService::Constants/WS_ERROR_CODE> for an example.
+
+=back
diff --git a/extensions/example/code/webservice-error_codes.pl b/extensions/example/code/webservice-error_codes.pl
new file mode 100644 (file)
index 0000000..94c4c52
--- /dev/null
@@ -0,0 +1,25 @@
+# -*- Mode: perl; indent-tabs-mode: nil -*-
+#
+# The contents of this file are subject to the Mozilla Public
+# License Version 1.1 (the "License"); you may not use this file
+# except in compliance with the License. You may obtain a copy of
+# the License at http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS
+# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+# implied. See the License for the specific language governing
+# rights and limitations under the License.
+#
+# The Original Code is the Bugzilla Bug Tracking System.
+#
+# The Initial Developer of the Original Code is Everything Solved, Inc.
+# Portions created by Everything Solved, Inc. are Copyright (C) 2008 
+# Everything Solved, Inc. All Rights Reserved.
+#
+# Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
+
+use strict;
+use warnings;
+use Bugzilla;
+my $error_map = Bugzilla->hook_args->{error_map};
+$error_map->{'example_my_error'} = 10001;
index ced4c6d369a8ae7a5e311be8b3da9db5fa03f3b2..146867294ce1c629f63842dc5d70dc35d0cc42a4 100644 (file)
 package extensions::example::lib::WSExample;
 use strict;
 use warnings;
-
 use base qw(Bugzilla::WebService);
+use Bugzilla::Error;
 
 # This can be called as Example.hello() from XML-RPC.
 sub hello { return 'Hello!'; }
 
+sub throw_an_error { ThrowUserError('example_my_error') }
+
 1;
diff --git a/extensions/example/template/en/global/user-error-errors.html.tmpl b/extensions/example/template/en/global/user-error-errors.html.tmpl
new file mode 100644 (file)
index 0000000..df5a203
--- /dev/null
@@ -0,0 +1,12 @@
+[%# Note that error messages should generally be indented four spaces, like
+  # below, because when Bugzilla translates an error message into plain
+  # text, it takes four spaces off the beginning of the lines. 
+  #
+  # Note also that I prefixed my error name with "example", the name of my
+  # extension, so that I wouldn't conflict with other error names in
+  # Bugzilla or other extensions.
+  #%]
+[% IF error == "example_my_error" %]
+   [% title = "Example Error Title" %]
+   This is the error message! It contains <em>some html</em>.
+[% END %]