org.mule.api.MessagingException Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mule-core Show documentation
Show all versions of mule-core Show documentation
Mule server and core classes
/*
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package org.mule.api;
import static java.lang.String.format;
import static org.apache.commons.lang.StringUtils.abbreviate;
import static org.mule.util.ClassUtils.isConsumable;
import org.mule.VoidMuleEvent;
import org.mule.api.processor.MessageProcessor;
import org.mule.config.DefaultMuleConfiguration;
import org.mule.config.ExceptionHelper;
import org.mule.config.i18n.Message;
import org.mule.routing.filters.RegExFilter;
import org.mule.routing.filters.WildcardFilter;
import org.mule.transport.NullPayload;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* MessagingException
is a general message exception thrown when errors
* specific to Message processing occur..
*/
public class MessagingException extends MuleException
{
public static final String PAYLOAD_INFO_KEY = "Payload";
public static final String PAYLOAD_TYPE_INFO_KEY = "Payload Type";
/**
* Serial version
*/
private static final long serialVersionUID = 6941498759267936649L;
/**
* The MuleMessage being processed when the error occurred
*/
protected transient MuleMessage muleMessage;
/**
* The MuleEvent being processed when the error occurred
*/
protected final transient MuleEvent event;
protected transient MuleEvent processedEvent;
private boolean causeRollback;
private boolean handled;
private transient MessageProcessor failingMessageProcessor;
/**
* @deprecated use MessagingException(Message, MuleEvent)
*/
@Deprecated
public MessagingException(Message message, MuleMessage muleMessage)
{
super();
this.muleMessage = muleMessage;
this.event = null;
setMessage(generateMessage(message));
}
public MessagingException(Message message, MuleEvent event)
{
super();
this.event = event;
extractMuleMessage(event);
setMessage(generateMessage(message));
}
public MessagingException(Message message, MuleEvent event, MessageProcessor failingMessageProcessor)
{
super();
this.event = event;
extractMuleMessage(event);
this.failingMessageProcessor = failingMessageProcessor;
setMessage(generateMessage(message));
}
/**
* @deprecated use MessagingException(Message, MuleEvent, Throwable)
*/
@Deprecated
public MessagingException(Message message, MuleMessage muleMessage, Throwable cause)
{
super(cause);
this.muleMessage = muleMessage;
this.event = null;
setMessage(generateMessage(message));
}
public MessagingException(Message message, MuleEvent event, Throwable cause)
{
super(cause);
this.event = event;
extractMuleMessage(event);
setMessage(generateMessage(message));
}
public MessagingException(Message message,
MuleEvent event,
Throwable cause,
MessageProcessor failingMessageProcessor)
{
super(cause);
this.event = event;
extractMuleMessage(event);
this.failingMessageProcessor = failingMessageProcessor;
setMessage(generateMessage(message));
}
public MessagingException(MuleEvent event, Throwable cause)
{
super(cause);
this.event = event;
extractMuleMessage(event);
setMessage(generateMessage(getI18nMessage()));
}
public MessagingException(MuleEvent event, Throwable cause, MessageProcessor failingMessageProcessor)
{
super(cause);
this.event = event;
extractMuleMessage(event);
this.failingMessageProcessor = failingMessageProcessor;
setMessage(generateMessage(getI18nMessage()));
}
protected String generateMessage(Message message)
{
StringBuilder buf = new StringBuilder(80);
if (message != null)
{
buf.append(message.getMessage()).append(".");
}
if (muleMessage != null)
{
if (DefaultMuleConfiguration.isVerboseExceptions())
{
Object payload = muleMessage.getPayload();
if (payload == null)
{
payload = NullPayload.getInstance();
}
if (isConsumable(muleMessage.getPayload().getClass()))
{
addInfo(PAYLOAD_INFO_KEY, abbreviate(payload.toString(), 1000));
}
else
{
addInfo(PAYLOAD_TYPE_INFO_KEY, muleMessage.getPayload().getClass().getName());
try
{
addInfo(PAYLOAD_INFO_KEY, muleMessage.getPayloadAsString());
}
catch (Exception e)
{
addInfo(PAYLOAD_INFO_KEY, format("%s while getting payload: %s", e.getClass().getName(), e.getMessage()));
}
}
}
}
else
{
buf.append("The current MuleMessage is null!");
addInfo(PAYLOAD_INFO_KEY, NullPayload.getInstance().toString());
}
return buf.toString();
}
/**
* @deprecated use {@link #getEvent().getMessage()} instead
*/
@Deprecated
public MuleMessage getMuleMessage()
{
if ((getEvent() != null))
{
return event.getMessage();
}
return muleMessage;
}
/**
* @return event associated with the exception
*/
public MuleEvent getEvent()
{
return processedEvent != null && !VoidMuleEvent.getInstance().equals(processedEvent)
? processedEvent
: event;
}
/**
* Sets the event that should be processed once this exception is caught
*
* @param processedEvent event bounded to the exception
*/
public void setProcessedEvent(MuleEvent processedEvent)
{
if (processedEvent != null && !VoidMuleEvent.getInstance().equals(processedEvent))
{
this.processedEvent = processedEvent;
this.muleMessage = this.processedEvent.getMessage();
}
else
{
this.processedEvent = null;
this.muleMessage = null;
}
}
/**
* Evaluates if the exception was caused (instance of) by the provided exception
* type
*
* @param e exception type to check against
* @return true if the cause exception is an instance of the provided exception
* type
*/
public boolean causedBy(final Class e)
{
if (e == null)
{
throw new IllegalArgumentException("Class cannot be null");
}
return (ExceptionHelper.traverseCauseHierarchy(this, new ExceptionHelper.ExceptionEvaluator