001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.bean.jsonrpc;
018
019
020import org.apache.juneau.marshall.*;
021import static org.apache.juneau.commons.utils.Shorts.*;
022
023/**
024 * Runtime exception carrying JSON-RPC error fields for mapping to {@link JsonRpcError}.
025 */
026@Marshalled
027@SuppressWarnings({
028   "java:S1165" // Fields have a public setter used by callers for chained mutation after construction.
029})
030public class McpException extends RuntimeException {
031
032   private static final long serialVersionUID = 1L;
033
034   private int code;
035   private transient Object data;
036
037   /**
038    * Constructor.
039    *
040    * @param code JSON-RPC error code.
041    * @param message Error message.  Can be <jk>null</jk> to leave the exception message unset.
042    */
043   public McpException(int code, String message) {
044      super(message);
045      this.code = code;
046   }
047
048   /**
049    * Constructor.
050    *
051    * @param code JSON-RPC error code.
052    * @param message Error message.  Can be <jk>null</jk> to leave the exception message unset.
053    * @param data Optional structured error data.  Can be <jk>null</jk> to leave the property unset.
054    */
055   public McpException(int code, String message, Object data) {
056      super(message);
057      this.code = code;
058      this.data = data;
059   }
060
061   /**
062    * JSON-RPC error code.
063    *
064    * @return The code.
065    */
066   public int getCode() {
067      return code;
068   }
069
070   /**
071    * Sets the JSON-RPC error code.
072    *
073    * @param value The new value.
074    * @return This object (for method chaining).
075    */
076   public McpException setCode(int value) {
077      code = value;
078      return this;
079   }
080
081   /**
082    * Optional structured error data.
083    *
084    * @return The data, or {@code null} if not set.
085    */
086   public Object getData() {
087      return data;
088   }
089
090   /**
091    * Sets optional structured error data.
092    *
093    * @param value The new value.  Can be <jk>null</jk> to unset the property.
094    * @return This object (for method chaining).
095    */
096   public McpException setData(Object value) {
097      data = value;
098      return this;
099   }
100
101   /**
102    * Converts this exception to a {@link JsonRpcError}.
103    *
104    * @return A new error object. Never {@code null}.
105    */
106   public JsonRpcError toJsonRpcError() {
107      return new JsonRpcError()
108         .setCode(code)
109         .setMessage(getMessage())
110         .setData(data);
111   }
112
113   /**
114    * Creates an {@link McpException} from a {@link JsonRpcError}.
115    *
116    * @param e The source error. Must not be <jk>null</jk>.
117    * @return A new exception containing the same code, message, and data.
118    */
119   public static McpException fromJsonRpcError(JsonRpcError e) {
120      var a = reqnn("error", e);
121      return new McpException(a.getCode(), a.getMessage(), a.getData());
122   }
123}