Skip to content

Commit

Permalink
Started improved error texts; #198
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Dec 7, 2023
1 parent d36d34e commit de3115b
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 144 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright (C) 2015-2023 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.helger.phase4.error;

import java.util.Locale;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.helger.commons.builder.IBuilder;
import com.helger.commons.string.StringHelper;
import com.helger.phase4.ebms3header.Ebms3Description;
import com.helger.phase4.ebms3header.Ebms3Error;
import com.helger.phase4.messaging.domain.MessageHelperMethods;

/**
* Builder class for {@link Ebms3Error}
*
* @author Philip Helger
* @since 2.6.0
*/
public class Ebms3ErrorBuilder implements IBuilder <Ebms3Error>
{
private Ebms3Description m_aDescription;
private String m_sErrorDetail;
private EEbmsErrorCategory m_eCategory;
private String m_sRefToMessageInError;
private String m_sErrorCode;
private String m_sOrigin;
private EEbmsErrorSeverity m_eSeverity;
private String m_sShortDescription;

public Ebms3ErrorBuilder ()
{}

@Nonnull
public Ebms3ErrorBuilder description (@Nullable final String s, @Nullable final Locale aLocale)
{
return description (StringHelper.hasNoText (s) || aLocale == null ? null : MessageHelperMethods
.createEbms3Description (aLocale,
s));
}

@Nonnull
public Ebms3ErrorBuilder description (@Nullable final Ebms3Description a)
{
m_aDescription = a;
return this;
}

@Nonnull
public Ebms3ErrorBuilder errorDetail (@Nullable final String s)
{
m_sErrorDetail = s;
return this;
}

@Nonnull
public Ebms3ErrorBuilder category (@Nullable final EEbmsErrorCategory e)
{
m_eCategory = e;
return this;
}

@Nonnull
public Ebms3ErrorBuilder refToMessageInError (@Nullable final String s)
{
m_sRefToMessageInError = s;
return this;
}

@Nonnull
public Ebms3ErrorBuilder errorCode (@Nullable final String s)
{
m_sErrorCode = s;
return this;
}

@Nonnull
public Ebms3ErrorBuilder origin (@Nullable final String s)
{
m_sOrigin = s;
return this;
}

@Nonnull
public Ebms3ErrorBuilder severity (@Nullable final EEbmsErrorSeverity e)
{
m_eSeverity = e;
return this;
}

@Nonnull
public Ebms3ErrorBuilder shortDescription (@Nullable final String s)
{
m_sShortDescription = s;
return this;
}

@Nonnull
public Ebms3Error build ()
{
if (m_eSeverity == null)
throw new IllegalStateException ("Severity is required");
if (StringHelper.hasNoText (m_sErrorCode))
throw new IllegalStateException ("Error Code is required");

final Ebms3Error aEbms3Error = new Ebms3Error ();
// Default to shortDescription if none provided
aEbms3Error.setDescription (m_aDescription);
aEbms3Error.setErrorDetail (m_sErrorDetail);
if (m_eCategory != null)
aEbms3Error.setCategory (m_eCategory.getDisplayName ());
aEbms3Error.setRefToMessageInError (m_sRefToMessageInError);
aEbms3Error.setErrorCode (m_sErrorCode);
aEbms3Error.setOrigin (m_sOrigin);
aEbms3Error.setSeverity (m_eSeverity.getSeverity ());
aEbms3Error.setShortDescription (m_sShortDescription);
return aEbms3Error;
}
}
55 changes: 46 additions & 9 deletions phase4-lib/src/main/java/com/helger/phase4/error/IEbmsError.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,52 @@ public interface IEbmsError
@Nonnull
EEbmsErrorCategory getCategory ();

/**
* Convert the EBMS Error into an {@link IError}.
*
* @param aContentLocale
* The locale used to resolve the error text, in case the text is
* multilingual. The locale may be ignored.
* @return The created {@link IError}.
*/
@Nonnull
@Deprecated (forRemoval = true, since = "2.6.0")
default IError getAsError (@Nonnull final Locale aContentLocale)
{
return getAsError (null, aContentLocale);
}

/**
* Convert the EBMS Error into an {@link IError}.
*
* @param sAdditionalErrorText
* An optional additional error text to be appended to the default
* text. May be <code>null</code>.
* @param aContentLocale
* The locale used to resolve the error text, in case the text is
* multilingual. The locale may be ignored.
* @return The created {@link IError}.
* @since 2.6.0
*/
@Nonnull
default IError getAsError (@Nullable final String sAdditionalErrorText, @Nonnull final Locale aContentLocale)
{
String sErrorText = "[" +
getCategory ().getDisplayName () +
"] " +
StringHelper.getNotNull (getErrorDetail ().getDisplayText (aContentLocale),
getShortDescription ());
if (StringHelper.hasText (sAdditionalErrorText))
{
if (!sErrorText.endsWith ("."))
sErrorText += '.';
sErrorText += sAdditionalErrorText;
}

return SingleError.builder ()
.errorLevel (getSeverity ().getErrorLevel ())
.errorID (getErrorCode ())
.errorText ("[" +
getCategory ().getDisplayName () +
"] " +
StringHelper.getNotNull (getErrorDetail ().getDisplayText (aContentLocale), getShortDescription ()))
.errorText (sErrorText)
.build ();
}

Expand All @@ -112,8 +148,9 @@ default Ebms3Error getAsEbms3Error (@Nonnull final Locale aContentLocale,
return getAsEbms3Error (aContentLocale,
sRefToMessageInError,
sOrigin,
sErrorDescription == null ? null
: MessageHelperMethods.createEbms3Description (aContentLocale, sErrorDescription));
sErrorDescription == null ? null : MessageHelperMethods.createEbms3Description (
aContentLocale,
sErrorDescription));
}

@Nonnull
Expand All @@ -124,9 +161,9 @@ default Ebms3Error getAsEbms3Error (@Nonnull final Locale aContentLocale,
{
final Ebms3Error aEbms3Error = new Ebms3Error ();
// Default to shortDescription if none provided
aEbms3Error.setDescription (aEbmsDescription != null ? aEbmsDescription
: MessageHelperMethods.createEbms3Description (aContentLocale,
getShortDescription ()));
aEbms3Error.setDescription (aEbmsDescription != null ? aEbmsDescription : MessageHelperMethods
.createEbms3Description (aContentLocale,
getShortDescription ()));
aEbms3Error.setErrorDetail (getErrorDetail ().getDisplayText (aContentLocale));
aEbms3Error.setErrorCode (getErrorCode ());
aEbms3Error.setSeverity (getSeverity ().getSeverity ());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,19 +488,13 @@ private static void _processSoapHeaderElements (@Nonnull final SOAPHeaderElement
final Locale aLocale = aState.getLocale ();
for (final IError aError : aErrorList)
{
final EEbmsError ePredefinedError = EEbmsError.getFromErrorCodeOrNull (aError.getErrorID ());
if (ePredefinedError != null)
aErrorMessages.add (ePredefinedError.getAsEbms3Error (aLocale, sRefToMessageID));
else
{
final Ebms3Error aEbms3Error = new Ebms3Error ();
aEbms3Error.setErrorDetail (aError.getErrorText (aLocale));
aEbms3Error.setErrorCode (aError.getErrorID ());
aEbms3Error.setSeverity (aError.getErrorLevel ().getID ());
aEbms3Error.setOrigin (aError.getErrorFieldName ());
aEbms3Error.setRefToMessageInError (sRefToMessageID);
aErrorMessages.add (aEbms3Error);
}
final Ebms3Error aEbms3Error = new Ebms3Error ();
aEbms3Error.setErrorDetail (aError.getErrorText (aLocale));
aEbms3Error.setErrorCode (aError.getErrorID ());
aEbms3Error.setSeverity (aError.getErrorLevel ().getID ());
aEbms3Error.setOrigin (aError.getErrorFieldName ());
aEbms3Error.setRefToMessageInError (sRefToMessageID);
aErrorMessages.add (aEbms3Error);
}

// Stop processing of other headers
Expand All @@ -511,14 +505,14 @@ private static void _processSoapHeaderElements (@Nonnull final SOAPHeaderElement
{
// upon failure, the element stays unprocessed and sends back a signal
// message with the errors
LOGGER.error ("Error processing SOAP header element " + aQName.toString () + " with processor " + aProcessor,
ex);

final String sDetails = "Error processing SOAP header element " +
aQName.toString () +
" with processor " +
aProcessor;
LOGGER.error (sDetails, ex);
aErrorMessages.add (EEbmsError.EBMS_OTHER.getAsEbms3Error (aState.getLocale (),
aState.getMessageID (),
"Error processing SOAP header element " +
aQName.toString ()));

sDetails));
// Stop processing of other headers
break;
}
Expand Down Expand Up @@ -667,21 +661,21 @@ public static IAS4MessageState processEbmsMessage (@Nonnull @WillNotClose final
(aEbmsError != null ? 1 : 0);
if (nCountData != 1)
{
final String sMsg = "Expected a UserMessage(" +
(aEbmsUserMessage != null ? 1 : 0) +
"), a PullRequest(" +
(aEbmsPullRequest != null ? 1 : 0) +
"), a Receipt(" +
(aEbmsReceipt != null ? 1 : 0) +
") or an Error(" +
(aEbmsError != null ? 1 : 0) +
")";
LOGGER.error (sMsg);
final String sDetails = "Expected a UserMessage(" +
(aEbmsUserMessage != null ? 1 : 0) +
"), a PullRequest(" +
(aEbmsPullRequest != null ? 1 : 0) +
"), a Receipt(" +
(aEbmsReceipt != null ? 1 : 0) +
") or an Error(" +
(aEbmsError != null ? 1 : 0) +
")";
LOGGER.error (sDetails);

// send EBMS:0001 error back
aErrorMessagesTarget.add (EEbmsError.EBMS_VALUE_NOT_RECOGNIZED.getAsEbms3Error (aLocale,
aState.getMessageID (),
sMsg));
sDetails));
}

// Determine AS4 profile ID (since 0.13.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,9 +847,12 @@ private void _invokeSPIsForIncoming (@Nonnull final HttpHeaderMap aHttpHeaders,
}
catch (final AS4DecompressException ex)
{
LOGGER.error ("Failed to decompress AS4 payload", ex);
final String sDetails = "Failed to decompress AS4 payload";
LOGGER.error (sDetails, ex);
// Hack for invalid GZip content from WSS4JAttachment.getSourceStream
aErrorMessagesTarget.add (EEbmsError.EBMS_DECOMPRESSION_FAILURE.getAsEbms3Error (m_aLocale, sMessageID));
aErrorMessagesTarget.add (EEbmsError.EBMS_DECOMPRESSION_FAILURE.getAsEbms3Error (m_aLocale,
sMessageID,
sDetails));
return;
}
catch (final RuntimeException ex)
Expand Down
Loading

0 comments on commit de3115b

Please # to comment.