Skip to content

Always add exception cause #2239

@hendrikebbers

Description

@hendrikebbers

Today Bouncy Castle uses several different ways/pattern how exceptions are handled. In general the best practice would be code like this:

try {
    doSomeMagic(); 
} catch(AnyException e) {
    throw new ConcreteException("Error in doing some magic!", e);
}

While many places of Bounce Castle follow that pattern some places are use other patterns like this:

try {
    doSomeMagic(); 
} catch(AnyException e) {
    throw new ConcreteException("Error in doing some magic:" + e.getMessage());
}

By doing so the original exception (including any possible cause and stack trace) got lost. Especially for application in production that relay on logging regarding errors, those information are very important to understand the cause of an exception.

There are even some other patterns in the code like this snippet from DerUtil.java:

static byte[] toByteArray(ASN1Primitive primitive)
    {
        try
        {
            return primitive.getEncoded();
        }
        catch (final IOException e)
        {
            throw new IllegalStateException("Cannot get encoding: " + e.getMessage())
            {
                public Throwable getCause()
                {
                    return e;
                }
            };
        }
    }

A good starting point to find the places that does not use the best practice pattern is by searching in the project for e.getMessage().

I would be more than happy to work on this issue and migrate all exception handling in Bounce Castle to the same pattern. If you agree on this please assign me to the issue and leave a comment :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions