How to get the error message in Controller or Route after handled exception in errorHandler?

laziestcoder :

I had to customized Sftp Inbound default handler LoggingHandler and using my own CustomizedErrorHandler which extends ErrorHandler. But I can't return any message to my controller after handling exceptions.

I was researching couple of days and I found nothing to show my customized message to my UI using Controller. Below are some code snippet from my CustomizedErrorHandler, SftpInboundConfiguration.

SftpInboundConfiguration

public IntegrationFlow fileFlow() {
    SftpInboundChannelAdapterSpec spec = Sftp
            .inboundAdapter(getSftpSessionFactory())
            .preserveTimestamp(true)
            .remoteDirectory(getSourceLocation())
            .autoCreateLocalDirectory(true)
            .deleteRemoteFiles(false)
            .localDirectory(new File(getDestinationLocation()));

    return IntegrationFlows
            .from(spec, e -> e.id(BEAN_ID)
                    .autoStartup(false)
                    .poller(sftpPoller())
            )
            .channel(sftpReceiverChannel())
            .handle(sftpInboundMessageHandler())
            .get();
}

... ... ...

public PollerMetadata sftpPoller() {
        PollerMetadata pollerMetadata = new PollerMetadata();
        List<Advice> adviceChain = new ArrayList<>();
        pollerMetadata.setErrorHandler(customErrorMessageHandler());
        pollerMetadata.setTrigger(new PeriodicTrigger(5000));
        return pollerMetadata;
}

... ... ...

private CustomErrorMessageHandler customErrorMessageHandler() {
        return new CustomErrorMessageHandler(
                controlChannel(),
                BEAN_ID
        );
}

CustomErrorMessageHandler

public class CustomErrorMessageHandler implements ErrorHandler {
    private final MessageChannel CONTROL_CHANNEL;
    private final String BEAN_ID;

    public CustomErrorMessageHandler(
                  MessageChannel controlChannel, 
                  String beanID
    ) {
        this.CONTROL_CHANNEL = controlChannel;
        this.BEAN_ID = beanID;
    }

    public void handleError(@NotNull Throwable throwable) {
        final Throwable rootCause = ExceptionUtils.getRootCause(throwable);
        if (rootCause instanceof MessagingException) {
            log.error("MessagingException : {} ", rootCause.getMessage());
        } else if (rootCause instanceof SftpException) {
            log.error("SftpException : {}", rootCause.getMessage());
        }   ... ... ... 
        else {
            log.error("Unknown : Cause : {} , Error : {}", 
                                 rootCause, rootCause.getMessage());
        }


        log.info("Stopping SFTP Inbound");
        boolean is_stopped = CONTROL_CHANNEL.send(
                 new GenericMessage<>("@" + BEAN_ID + ".stop()"));
        if (is_stopped) {
            log.info("SFTP Inbound Stopped.");
        } else {
            log.info("SFTP Inbound Stop Failed.");
        }
    }
}

Now I want to save some customized message from if-else statements and need to show it in UI. Is there any way to save the message and show it using Route or Controller ?

Gary Russell :

Don't customize the error handler, use poller.errorChannel("myErrorChannel") instead.

Then add an error channel flow

@Bean
IntegrationFlow errors() {
    return IntegrationFLows.from("myErrorChannel")
        .handle(...)
        ...
        .get();

The message sent to the handler is an ErrorMessage with a MessagingException payload, with cause and failedMessage which was the message at the point of the failure and originalMessage which is the original message emitted by the adapter.

After handling the exception, you can simply call a method on your controller to tell it the state.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to display error message instead of java exception?

How to get the error message in Controller or Route after handling exception in errorHandler?

How return error message in spring mvc @Controller

How to get exception message in Python properly

How to get the last exception object after an error is raised at a Python prompt?

How to get error message after entity validation

How to print a message after capturing an exception in pytest

Why does the constructor handle the exception but I get a "not handled exception error" when I use it

How to catch exception and send custom error message

How to print the error message of an exception of errorUserTitle

Code after Splitter with aggregation strategy is not executed if exception in inner route were handled (Apache Camel)

How do I get errors which are handled serverside in a ajax error message after submitting a form?

update get error in catch exception and the message is in the description

Exception message is been displaying in the textbox which is handled in the function should be changed to a simple error message.

How to access only the exception error message in R

How to do localization of error message in controller

Display exception message handled by Spring using JSP

Slim - Swift Mailer on the route is work but if after moved on controller get errors

How to re-route exception without marking Exception as handled

How to get route value to controller in angular js

How get original message after get an errorHandler and write a file

How to show success message below html form after form submit which is being handled by different php file

Why I get the "(node:7424) UnhandledPromiseRejectionWarning" message for handled error?

How to get translate message in controller Laravel?

How to write an if statement for a specific error message in an exception?

How do I get the Exception class that was thrown in my spring error controller

How to raise a message box in case of not handled Error in Python?

How a custom exception handled responses message could be trimmed into shorter one for a Spring Boot Project?

SWR NextJS error doesn't get handled after throwing