Bug 1919083 - [ci] Enable os-integration variant for more suites, r=jmaher
[gecko.git] / xpcom / base / ErrorNames.cpp
blob42401aa86023cabfc5b00306aacb16b9ce1df6e2
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/. */
7 #include "mozilla/ArrayUtils.h"
8 #include "mozilla/ErrorNames.h"
9 #include "nsString.h"
10 #include "prerror.h"
11 #include "MainThreadUtils.h"
13 // Get the GetErrorNameInternal method
14 #include "ErrorNamesInternal.h"
16 namespace mozilla {
18 const char* GetStaticErrorName(nsresult rv) { return GetErrorNameInternal(rv); }
20 void GetErrorName(nsresult rv, nsACString& name) {
21 if (const char* errorName = GetErrorNameInternal(rv)) {
22 name.AssignASCII(errorName);
23 return;
26 bool isSecurityError = NS_ERROR_GET_MODULE(rv) == NS_ERROR_MODULE_SECURITY;
28 // NS_ERROR_MODULE_SECURITY is the only module that is "allowed" to
29 // synthesize nsresult error codes that are not listed in ErrorList.h. (The
30 // NS_ERROR_MODULE_SECURITY error codes are synthesized from NSPR error
31 // codes.)
32 MOZ_ASSERT(isSecurityError);
34 if (NS_SUCCEEDED(rv)) {
35 name.AssignLiteral("NS_ERROR_GENERATE_SUCCESS(");
36 } else {
37 name.AssignLiteral("NS_ERROR_GENERATE_FAILURE(");
40 if (isSecurityError) {
41 name.AppendLiteral("NS_ERROR_MODULE_SECURITY");
42 } else {
43 // This should never happen given the assertion above, so we don't bother
44 // trying to print a symbolic name for the module here.
45 name.AppendInt(NS_ERROR_GET_MODULE(rv));
48 name.AppendLiteral(", ");
50 const char* nsprName = nullptr;
51 if (isSecurityError && NS_IsMainThread()) {
52 // Invert the logic from NSSErrorsService::GetXPCOMFromNSSError
53 PRErrorCode nsprCode = -1 * static_cast<PRErrorCode>(NS_ERROR_GET_CODE(rv));
54 nsprName = PR_ErrorToName(nsprCode);
56 // All NSPR error codes defined by NSPR or NSS should have a name mapping.
57 MOZ_ASSERT(nsprName);
60 if (nsprName) {
61 name.AppendASCII(nsprName);
62 } else {
63 name.AppendInt(NS_ERROR_GET_CODE(rv));
66 name.AppendLiteral(")");
69 } // namespace mozilla
71 extern "C" {
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 Gecko_GetErrorName(nsresult aRv, nsACString& aName) {
76 mozilla::GetErrorName(aRv, aName);