Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "mozilla/ArrayUtils.h"
8 : #include "mozilla/ErrorNames.h"
9 : #include "nsString.h"
10 : #include "prerror.h"
11 :
12 : // Get the GetErrorNameInternal method
13 : #include "ErrorNamesInternal.h"
14 :
15 : namespace mozilla {
16 :
17 : void
18 0 : GetErrorName(nsresult rv, nsACString& name)
19 : {
20 0 : if (const char* errorName = GetErrorNameInternal(rv)) {
21 0 : name.AssignASCII(errorName);
22 0 : return;
23 : }
24 :
25 0 : bool isSecurityError = NS_ERROR_GET_MODULE(rv) == NS_ERROR_MODULE_SECURITY;
26 :
27 : // NS_ERROR_MODULE_SECURITY is the only module that is "allowed" to
28 : // synthesize nsresult error codes that are not listed in ErrorList.h. (The
29 : // NS_ERROR_MODULE_SECURITY error codes are synthesized from NSPR error
30 : // codes.)
31 0 : MOZ_ASSERT(isSecurityError);
32 :
33 0 : name.AssignASCII(NS_SUCCEEDED(rv) ? "NS_ERROR_GENERATE_SUCCESS("
34 0 : : "NS_ERROR_GENERATE_FAILURE(");
35 :
36 0 : if (isSecurityError) {
37 0 : name.AppendASCII("NS_ERROR_MODULE_SECURITY");
38 : } else {
39 : // This should never happen given the assertion above, so we don't bother
40 : // trying to print a symbolic name for the module here.
41 0 : name.AppendInt(NS_ERROR_GET_MODULE(rv));
42 : }
43 :
44 0 : name.AppendASCII(", ");
45 :
46 0 : const char * nsprName = nullptr;
47 0 : if (isSecurityError) {
48 : // Invert the logic from NSSErrorsService::GetXPCOMFromNSSError
49 : PRErrorCode nsprCode
50 0 : = -1 * static_cast<PRErrorCode>(NS_ERROR_GET_CODE(rv));
51 0 : nsprName = PR_ErrorToName(nsprCode);
52 :
53 : // All NSPR error codes defined by NSPR or NSS should have a name mapping.
54 0 : MOZ_ASSERT(nsprName);
55 : }
56 :
57 0 : if (nsprName) {
58 0 : name.AppendASCII(nsprName);
59 : } else {
60 0 : name.AppendInt(NS_ERROR_GET_CODE(rv));
61 : }
62 :
63 0 : name.AppendASCII(")");
64 : }
65 :
66 : } // namespace mozilla
67 :
68 : extern "C" {
69 :
70 : // This is an extern "C" binding for the GetErrorName method which is used by
71 : // the nsresult rust bindings in xpcom/rust/nserror.
72 : void
73 0 : Gecko_GetErrorName(nsresult aRv, nsACString& aName)
74 : {
75 0 : GetErrorName(aRv, aName);
76 0 : }
77 :
78 : }
|