Bug 1950824 - Add nsINSSErrorsService.getErrorName(). r=keeler

Differential Revision: https://phabricator.services.mozilla.com/D239879
This commit is contained in:
Kai Engert
2025-03-04 13:45:28 +00:00
parent c61dd19ea9
commit 08ccc4ebd9
4 changed files with 94 additions and 14 deletions

View File

@@ -179,25 +179,36 @@ static const char* getOverrideErrorStringName(PRErrorCode aErrorCode) {
}
}
mozilla::Result<PRErrorCode, nsresult> NSResultToPRErrorCode(
nsresult aXPCOMErrorCode) {
if (NS_ERROR_GET_MODULE(aXPCOMErrorCode) != NS_ERROR_MODULE_SECURITY ||
NS_ERROR_GET_SEVERITY(aXPCOMErrorCode) != NS_ERROR_SEVERITY_ERROR) {
return Err(NS_ERROR_FAILURE);
}
PRErrorCode nsprCode = -1 * NS_ERROR_GET_CODE(aXPCOMErrorCode);
if (!mozilla::psm::IsNSSErrorCode(nsprCode)) {
return Err(NS_ERROR_FAILURE);
}
return nsprCode;
}
NS_IMETHODIMP
NSSErrorsService::GetErrorMessage(nsresult aXPCOMErrorCode,
nsAString& aErrorMessage) {
if (NS_ERROR_GET_MODULE(aXPCOMErrorCode) != NS_ERROR_MODULE_SECURITY ||
NS_ERROR_GET_SEVERITY(aXPCOMErrorCode) != NS_ERROR_SEVERITY_ERROR) {
return NS_ERROR_FAILURE;
auto prErrorCode = NSResultToPRErrorCode(aXPCOMErrorCode);
if (!prErrorCode.isOk()) {
return prErrorCode.unwrapErr();
}
int32_t aNSPRCode = -1 * NS_ERROR_GET_CODE(aXPCOMErrorCode);
if (!mozilla::psm::IsNSSErrorCode(aNSPRCode)) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIStringBundle> theBundle = mPIPNSSBundle;
const char* idStr = getOverrideErrorStringName(aNSPRCode);
if (!idStr) {
idStr = PR_ErrorToName(aNSPRCode);
nsCOMPtr<nsIStringBundle> theBundle;
const char* idStr = getOverrideErrorStringName(prErrorCode.unwrap());
if (idStr) {
theBundle = mPIPNSSBundle;
} else {
idStr = PR_ErrorToName(prErrorCode.unwrap());
theBundle = mNSSErrorsBundle;
}
@@ -213,5 +224,22 @@ NSSErrorsService::GetErrorMessage(nsresult aXPCOMErrorCode,
return rv;
}
NS_IMETHODIMP
NSSErrorsService::GetErrorName(nsresult aXPCOMErrorCode,
nsAString& aErrorName) {
auto prErrorCode = NSResultToPRErrorCode(aXPCOMErrorCode);
if (!prErrorCode.isOk()) {
return prErrorCode.unwrapErr();
}
const char* idStr = PR_ErrorToName(prErrorCode.unwrap());
if (!idStr) {
return NS_ERROR_FAILURE;
}
aErrorName = NS_ConvertASCIItoUTF16(idStr);
return NS_OK;
}
} // namespace psm
} // namespace mozilla

View File

@@ -31,6 +31,13 @@ interface nsINSSErrorsService : nsISupports
*/
AString getErrorMessage(in nsresult aXPCOMErrorCode);
/**
* Function will fail if aXPCOMErrorCode is not an NSS error code.
* @param aXPCOMErrorCode An error code obtained using getXPCOMFromNSSError
* return The human readable error name.
*/
AString getErrorName(in nsresult aXPCOMErrorCode);
/**
* Function will fail if aXPCOMErrorCode is not an NSS error code.
* @param aXPCOMErrorCode An error code obtained using getXPCOMFromNSSError

View File

@@ -0,0 +1,43 @@
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
"use strict";
// Get a profile directory and ensure PSM initializes NSS,
// to ensure the error string tables are installed.
do_get_profile();
Cc["@mozilla.org/psm;1"].getService(Ci.nsISupports);
function run_test() {
let nssErrorsService = Cc["@mozilla.org/nss_errors_service;1"].getService(
Ci.nsINSSErrorsService
);
let xpcom = nssErrorsService.getXPCOMFromNSSError(SEC_ERROR_UNTRUSTED_CERT);
let name = nssErrorsService.getErrorName(xpcom);
equal(
name,
"SEC_ERROR_UNTRUSTED_CERT",
"GetErrorName should work for SEC errors"
);
xpcom = nssErrorsService.getXPCOMFromNSSError(SSL_ERROR_BAD_CERT_DOMAIN);
name = nssErrorsService.getErrorName(xpcom);
equal(
name,
"SSL_ERROR_BAD_CERT_DOMAIN",
"GetErrorName should work for SSL errors"
);
xpcom = nssErrorsService.getXPCOMFromNSSError(
MOZILLA_PKIX_ERROR_INSUFFICIENT_CERTIFICATE_TRANSPARENCY
);
name = nssErrorsService.getErrorName(xpcom);
equal(
name,
"MOZILLA_PKIX_ERROR_INSUFFICIENT_CERTIFICATE_TRANSPARENCY",
"GetErrorName should work for PKIX errors"
);
}

View File

@@ -355,3 +355,5 @@ skip-if = ["condprof"] # Bug 1769154 - as designed
run-sequentially = "hardcoded ports"
["test_x509.js"]
["test_nss_errors_service.js"]