/* Formats an error message for non-certificate-related SSL errors
 * and non-overridable certificate errors (both are of type
 * PlainErrormMessage). Use formatOverridableCertErrorMessage
 * for overridable cert errors.
 */
static nsresult
formatPlainErrorMessage(const nsXPIDLCString &host, int32_t port,
                        PRErrorCode err,
                        bool suppressPort443,
                        nsString &returnedMessage)
{
    static NS_DEFINE_CID(kNSSComponentCID, NS_NSSCOMPONENT_CID);

    const char16_t *params[1];
    nsresult rv;

    nsCOMPtr<nsINSSComponent> component = do_GetService(kNSSComponentCID, &rv);
    NS_ENSURE_SUCCESS(rv, rv);

    if (host.Length())
    {
        nsString hostWithPort;

        // For now, hide port when it's 443 and we're reporting the error.
        // In the future a better mechanism should be used
        // to make a decision about showing the port number, possibly by requiring
        // the context object to implement a specific interface.
        // The motivation is that Mozilla browser would like to hide the port number
        // in error pages in the common case.

        hostWithPort.AssignASCII(host);
        if (!suppressPort443 || port != 443) {
            hostWithPort.Append(':');
            hostWithPort.AppendInt(port);
        }
        params[0] = hostWithPort.get();

        nsString formattedString;
        rv = component->PIPBundleFormatStringFromName("SSLConnectionErrorPrefix",
                params, 1,
                formattedString);
        if (NS_SUCCEEDED(rv))
        {
            returnedMessage.Append(formattedString);
            returnedMessage.AppendLiteral("\n\n");
        }
    }

    nsString explanation;
    rv = nsNSSErrors::getErrorMessageFromCode(err, component, explanation);
    if (NS_SUCCEEDED(rv))
        returnedMessage.Append(explanation);

    return NS_OK;
}