|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2018 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.driver.internal.util; |
| 20 | + |
| 21 | +import io.netty.channel.ChannelHandlerContext; |
| 22 | +import io.netty.handler.codec.MessageToMessageEncoder; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.neo4j.driver.internal.messaging.ResetMessage; |
| 27 | +import org.neo4j.driver.internal.messaging.RunMessage; |
| 28 | + |
| 29 | +public class ThrowingMessageEncoder<T> extends MessageToMessageEncoder<T> |
| 30 | +{ |
| 31 | + private final RuntimeException error; |
| 32 | + |
| 33 | + private ThrowingMessageEncoder( Class<T> messageType, RuntimeException error ) |
| 34 | + { |
| 35 | + super( messageType ); |
| 36 | + this.error = error; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void encode( ChannelHandlerContext ctx, T msg, List<Object> out ) |
| 41 | + { |
| 42 | + ctx.pipeline().remove( this ); |
| 43 | + throw error; |
| 44 | + } |
| 45 | + |
| 46 | + public static ThrowingMessageEncoder<RunMessage> forRunMessage( RuntimeException error ) |
| 47 | + { |
| 48 | + return new ThrowingMessageEncoder<>( RunMessage.class, error ); |
| 49 | + } |
| 50 | + |
| 51 | + public static ThrowingMessageEncoder<ResetMessage> forResetMessage( RuntimeException error ) |
| 52 | + { |
| 53 | + return new ThrowingMessageEncoder<>( ResetMessage.class, error ); |
| 54 | + } |
| 55 | +} |
0 commit comments