001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.socket.messaging;
018
019import org.springframework.lang.Nullable;
020import org.springframework.messaging.Message;
021
022/**
023 * A contract for handling sub-protocol errors sent to clients.
024 *
025 * @author Rossen Stoyanchev
026 * @since 4.2
027 * @param <P> the message payload type
028 */
029public interface SubProtocolErrorHandler<P> {
030
031        /**
032         * Handle errors thrown while processing client messages providing an
033         * opportunity to prepare the error message or to prevent one from being sent.
034         * <p>Note that the STOMP protocol requires a server to close the connection
035         * after sending an ERROR frame. To prevent an ERROR frame from being sent,
036         * a handler could return {@code null} and send a notification message
037         * through the broker instead, e.g. via a user destination.
038         * @param clientMessage the client message related to the error, possibly
039         * {@code null} if error occurred while parsing a WebSocket message
040         * @param ex the cause for the error, never {@code null}
041         * @return the error message to send to the client, or {@code null} in which
042         * case no message will be sent.
043         */
044        @Nullable
045        Message<P> handleClientMessageProcessingError(@Nullable Message<P> clientMessage, Throwable ex);
046
047        /**
048         * Handle errors sent from the server side to clients, e.g. errors from the
049         * {@link org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler
050         * "broke relay"} because connectivity failed or the external broker sent an
051         * error message, etc.
052         * @param errorMessage the error message, never {@code null}
053         * @return the error message to send to the client, or {@code null} in which
054         * case no message will be sent.
055         */
056        @Nullable
057        Message<P> handleErrorMessageToClient(Message<P> errorMessage);
058
059}