7fc1682ba73b2686ed45b35d49ea836f85e7c587
[vpp.git] / src / vpp-api / java / jvpp-registry / io / fd / vpp / jvpp / VppBaseCallException.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.vpp.jvpp;
18
19
20 /**
21  * Base exception representing failed operation of JVpp request call
22  */
23 public abstract class VppBaseCallException extends Exception {
24     private final String methodName;
25     private final int errorCode;
26
27     /**
28      * Constructs an VppCallbackException with the specified api method name and error code.
29      *
30      * @param methodName name of a method, which invocation or execution failed
31      * @param errorCode  negative error code value associated with this failure
32      * @throws NullPointerException     if apiMethodName is null
33      */
34     public VppBaseCallException(final String methodName, final int errorCode) {
35         super(String.format("vppApi.%s failed with error code: %d", methodName, errorCode));
36         this.methodName = java.util.Objects.requireNonNull(methodName, "apiMethodName is null!");
37         this.errorCode = errorCode;
38         if(errorCode >= 0) {
39             throw new IllegalArgumentException("Error code must be < 0. Was " + errorCode +
40                     " for " + methodName );
41         }
42     }
43
44     /**
45      * Constructs an VppCallbackException with the specified api method name, error description and error code.
46      *
47      * @param methodName name of a method, which invocation or execution failed
48      * @param message    description of error reason
49      * @param errorCode  negative error code value associated with this failure
50      * @throws NullPointerException     if apiMethodName is null
51      */
52     public VppBaseCallException(final String methodName, final String message, final int errorCode) {
53         super(String.format("vppApi.%s failed: %s (error code: %d)", methodName,message, errorCode));
54         this.methodName = java.util.Objects.requireNonNull(methodName, "apiMethodName is null!");
55         this.errorCode = errorCode;
56         if(errorCode >= 0) {
57             throw new IllegalArgumentException("Error code must be < 0. Was " + errorCode +
58                     " for " + methodName );
59         }
60     }
61
62     /**
63      * Returns  name of a method, which invocation failed.
64      *
65      * @return method name
66      */
67     public String getMethodName() {
68         return methodName;
69     }
70
71     /**
72      * Returns the error code associated with this failure.
73      *
74      * @return a negative integer error code
75      */
76     public int getErrorCode() {
77         return errorCode;
78     }
79 }