View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.handler.codec;
17  
18  /**
19   * Thrown if an unsupported message is received by a codec.
20   */
21  public class UnsupportedMessageTypeException extends CodecException {
22  
23      private static final long serialVersionUID = 2799598826487038726L;
24  
25      public UnsupportedMessageTypeException(
26              Object message, Class<?>... expectedTypes) {
27          super(message(
28                  message == null? "null" : message.getClass().getName(), expectedTypes));
29      }
30  
31      public UnsupportedMessageTypeException() { }
32  
33      public UnsupportedMessageTypeException(String message, Throwable cause) {
34          super(message, cause);
35      }
36  
37      public UnsupportedMessageTypeException(String s) {
38          super(s);
39      }
40  
41      public UnsupportedMessageTypeException(Throwable cause) {
42          super(cause);
43      }
44  
45      private static String message(
46              String actualType, Class<?>... expectedTypes) {
47          StringBuilder buf = new StringBuilder(actualType);
48  
49          if (expectedTypes != null && expectedTypes.length > 0) {
50              buf.append(" (expected: ").append(expectedTypes[0].getName());
51              for (int i = 1; i < expectedTypes.length; i ++) {
52                  Class<?> t = expectedTypes[i];
53                  if (t == null) {
54                      break;
55                  }
56                  buf.append(", ").append(t.getName());
57              }
58              buf.append(')');
59          }
60  
61          return buf.toString();
62      }
63  }