/src/mozilla-central/xpcom/base/ErrorNames.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | GetErrorName(nsresult rv, nsACString& name) |
19 | 0 | { |
20 | 0 | if (const char* errorName = GetErrorNameInternal(rv)) { |
21 | 0 | name.AssignASCII(errorName); |
22 | 0 | return; |
23 | 0 | } |
24 | 0 | |
25 | 0 | bool isSecurityError = NS_ERROR_GET_MODULE(rv) == NS_ERROR_MODULE_SECURITY; |
26 | 0 |
|
27 | 0 | // NS_ERROR_MODULE_SECURITY is the only module that is "allowed" to |
28 | 0 | // synthesize nsresult error codes that are not listed in ErrorList.h. (The |
29 | 0 | // NS_ERROR_MODULE_SECURITY error codes are synthesized from NSPR error |
30 | 0 | // codes.) |
31 | 0 | MOZ_ASSERT(isSecurityError); |
32 | 0 |
|
33 | 0 | if (NS_SUCCEEDED(rv)) { |
34 | 0 | name.AssignLiteral("NS_ERROR_GENERATE_SUCCESS("); |
35 | 0 | } else { |
36 | 0 | name.AssignLiteral("NS_ERROR_GENERATE_FAILURE("); |
37 | 0 | } |
38 | 0 |
|
39 | 0 | if (isSecurityError) { |
40 | 0 | name.AppendLiteral("NS_ERROR_MODULE_SECURITY"); |
41 | 0 | } else { |
42 | 0 | // This should never happen given the assertion above, so we don't bother |
43 | 0 | // trying to print a symbolic name for the module here. |
44 | 0 | name.AppendInt(NS_ERROR_GET_MODULE(rv)); |
45 | 0 | } |
46 | 0 |
|
47 | 0 | name.AppendLiteral(", "); |
48 | 0 |
|
49 | 0 | const char * nsprName = nullptr; |
50 | 0 | if (isSecurityError) { |
51 | 0 | // Invert the logic from NSSErrorsService::GetXPCOMFromNSSError |
52 | 0 | PRErrorCode nsprCode |
53 | 0 | = -1 * static_cast<PRErrorCode>(NS_ERROR_GET_CODE(rv)); |
54 | 0 | nsprName = PR_ErrorToName(nsprCode); |
55 | 0 |
|
56 | 0 | // All NSPR error codes defined by NSPR or NSS should have a name mapping. |
57 | 0 | MOZ_ASSERT(nsprName); |
58 | 0 | } |
59 | 0 |
|
60 | 0 | if (nsprName) { |
61 | 0 | name.AppendASCII(nsprName); |
62 | 0 | } else { |
63 | 0 | name.AppendInt(NS_ERROR_GET_CODE(rv)); |
64 | 0 | } |
65 | 0 |
|
66 | 0 | name.AppendLiteral(")"); |
67 | 0 | } |
68 | | |
69 | | } // namespace mozilla |
70 | | |
71 | | extern "C" { |
72 | | |
73 | | // This is an extern "C" binding for the GetErrorName method which is used by |
74 | | // the nsresult rust bindings in xpcom/rust/nserror. |
75 | | void |
76 | | Gecko_GetErrorName(nsresult aRv, nsACString& aName) |
77 | 0 | { |
78 | 0 | mozilla::GetErrorName(aRv, aName); |
79 | 0 | } |
80 | | |
81 | | } |