Handle Exceptions

Updated at 1685685417000
EzyFox Server provides 3 way to help you handle exception easily

1. Use default exception

EzyFox Server provide to you EzyBadRequestException as a default exception. It will handle this exception and response the err command to client. Example this code fragment:

    @EzyDoHandle(Commands.CHAT_TO_ME)
    public ChatResponse chatToMe(ChatRequest request) {
        if (EzyStrings.isBlank(request.getWho())) {
            throw new EzyBadRequestException(400, "badRequest");
        }
        return new ChatResponse(greeting.greet(request.getWho()));
    }

EzyFox Server will catch the exception and handle it to return to client command error with data like this:

\[400, badRequest\]

You can find out full source code in ChatRequestController example.

2. Handle exception in a controller

If your exception only happends in a controller scope, you should handle it in that controller like this:

    @EzyRequestController
    public class ChatRequestController extends EzyLoggable {
        // request handlers
        @EzyTryCatch(BadWhoRequestException.class)
        public void handle(
                BadWhoRequestException e,
                EzyUser user,
                String cmd, 
                Object data) {
            logger.error("try cath BadWhoRequestException, cmd: {}, data: {}", cmd, data, e);
            responseFactory.newObjectResponse()
                .command(EzyResponseCommands.ERROR)
                .data(EzyEntityObjects.newObject("who", "invalid"))
                .user(user)
                .execute();
        }
    }

You can find out full source code in ChatRequestController example.

3. Handle exceptions in a global handler

If your exceptions happen in many controller, and you want to save time and avoid source code duplication, you can create a global handler class to handle that exceptions like this:

    @AllArgsConstructor
    @EzyExceptionHandler
    public class GlobalExceptionHandler extends EzyLoggable {
        private final EzyResponseFactory responseFactory;
        @EzyTryCatch(InvalidChatRequestException.class)
        public void handle(
            InvalidChatRequestException e,
            EzyUserSessionEvent event,
            String command,
            Object request
        ) {
            logger.info("invalid chat request, command: {}, data: {}", command, request, e);
            responseFactory.newObjectResponse()
                .command(EzyResponseCommands.ERROR)
                .data(EzyEntityObjects.newObject("request", "invalid"))
                .session(event.getSession())
                .execute();
        }
        @EzyTryCatch(IllegalArgumentException.class)
        public void handle(
                IllegalArgumentException e,
                EzySession session,
                String cmd, 
                @EzyRequestData Object data
        ) {
            logger.error("try cath IllegalArgumentException, cmd: {}, data: {}", cmd, data, e);
            responseFactory.newObjectResponse()
                .command(EzyResponseCommands.ERROR)
                .data(EzyEntityObjects.newObject("arguments", "invalid"))
                .session(session)
                .execute();
        }
        @EzyTryCatch(Exception.class)
        public void handle(
                Exception e,
                String cmd, 
                @EzyRequestData Object data
        ) throws Exception {
            throw e;
        }
    }

You can find out full source code in GlobalExceptionHandler example.

Next step

You can choose a way to login to EzyFox Server