aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/ErrorPolicy.java
blob: 4cee9c3ffae4e8007a6fdcadcd859d6cf3d66e92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi.messagebus.protocol;

import com.yahoo.messagebus.EmptyReply;
import com.yahoo.messagebus.routing.RoutingContext;

/**
 * This policy assigns an error supplied at constructor time to the routing context when {@link #select(RoutingContext)}
 * is invoked. This is useful for returning error states to the client instead of those auto-generated by mbus when a
 * routing policy can not be created.
 *
 * @author Simon Thoresen Hult
 */
public class ErrorPolicy implements DocumentProtocolRoutingPolicy {

    private final String msg;

    /**
     * Creates a new policy that will assign an {@link EmptyReply} with the given error to all routing contexts that
     * invoke {@link #select(RoutingContext)}.
     *
     * @param msg The message of the error to assign.
     */
    public ErrorPolicy(String msg) {
        this.msg = msg;
    }

    public void select(RoutingContext ctx) {
        ctx.setError(DocumentProtocol.ERROR_POLICY_FAILURE, msg);
    }

    public void merge(RoutingContext ctx) {
        throw new AssertionError("Routing should not pass terminated selection.");
    }

    public void destroy() {
    }
}