Skip to content

Filter MessageReceived中throw Exception没有执行Handler的ExceptionCaught #27

Description

@ZhanPeng

init

_connector = new AsyncSocketConnector();

            _connector.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8)));
            _connector.FilterChain.AddLast("logger", new Log4UnityFilter());
            _connector.FilterChain.AddLast("json", new JsonFilter());
            _connector.FilterChain.AddLast("error", new ErrorMessageFilter());
            _connector.FilterChain.AddLast("identity", new IdentityFilter(deviceType));
            
            _connector.Handler = new SyncSendReceiveHandler();

Filter

public class ErrorMessageFilter : IoFilterAdapter
    {
        public override void MessageReceived(INextFilter nextFilter, IoSession session, object message)
        {
            ResponseMessage response = message as ResponseMessage;
            if (response == null)
                nextFilter.ExceptionCaught(session, new Exception("Wrong Used"));
            
            if(response.Error != null)
            {
                Exception cause = new Exception("[code]:" + response.Error.Code + " [message]:" + response.Error.Message);
                throw cause; // HERE
                nextFilter.ExceptionCaught(session, cause);
            }
            
            nextFilter.MessageReceived(session, message);
        }
    }

并没有执行Handler
当throw Exception时,进入到DefaultIoFilterChain中的

private void CallNext(IEntry<IoFilter, INextFilter> entry, Action<IoFilter, INextFilter> act, Action<Exception> error = null)
    {
      try
      {
        IoFilter filter = entry.Filter;
        INextFilter nextFilter = entry.NextFilter;
        act(filter, nextFilter);
      }
      catch (Exception ex)
      {
        if (error == null)
          
          this.FireExceptionCaught(ex);
        else
          error(ex);
      }
    }

进入FireExceptionCaught(ex),但是之后过后,并没有进入Handler

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions