/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.plugin.pinot;

import io.trino.spi.TrinoException;

import java.util.Optional;

import static java.util.Objects.requireNonNull;

public class PinotException
        extends TrinoException
{
    private final Optional<String> query;
    private final boolean retryable;

    public PinotException(PinotErrorCode errorCode, Optional<String> query, String message)
    {
        this(errorCode, query, message, false, null);
    }

    public PinotException(PinotErrorCode errorCode, Optional<String> query, String message, boolean retryable)
    {
        this(errorCode, query, message, retryable, null);
    }

    public PinotException(PinotErrorCode errorCode, Optional<String> query, String message, Throwable throwable)
    {
        this(errorCode, query, message, false, throwable);
    }

    public PinotException(PinotErrorCode errorCode, Optional<String> query, String message, boolean retryable, Throwable throwable)
    {
        super(requireNonNull(errorCode, "errorCode is null"), requireNonNull(message, "message is null"), throwable);
        this.retryable = retryable;
        this.query = requireNonNull(query, "query is null");
    }

    public boolean isRetryable()
    {
        return retryable;
    }

    @Override
    public String getMessage()
    {
        String message = super.getMessage();
        if (query.isPresent()) {
            message += " with query \"" + query.get() + "\"";
        }
        return message;
    }
}

@@ -766,50 +766,8 @@
 n%0A%7B%0A
-    private final Optional%3CString%3E query;%0A
     
@@ -1530,49 +1530,36 @@
 %22), 
-requireNonNull(message
+formatMessage(query
 , 
-%22
 message
- is null%22
 ), t
@@ -1609,69 +1609,8 @@
 le;%0A
-        this.query = requireNonNull(query, %22query is null%22);%0A
     
@@ -1692,36 +1692,75 @@
     
-@Override%0A    public
+private static String formatMessage(Optional%3CString%3E query,
  String 
 getM
@@ -1755,27 +1755,23 @@
  String 
-getM
+m
 essage
-(
 )%0A    %7B%0A
@@ -1782,45 +1782,101 @@
     
-String message = super.getMessage(
+requireNonNull(query, %22query is null%22);%0A        requireNonNull(message, %22message is null%22
 );%0A
+%0A
     

Format exception message eagerly