/*
 * Copyright 2008-present MongoDB, Inc.
 *
 * 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 com.mongodb;

import com.mongodb.lang.Nullable;

/**
 * Top level Exception for all Exceptions, server-side or client-side, that come from the driver.
 */
public class MongoException extends RuntimeException {
    private static final long serialVersionUID = -4415279469780082174L;

    private final int code;

    /**
     * Static helper to create or cast a MongoException from a throwable
     *
     * @param t a throwable, which may be null
     * @return a MongoException
     */
    @Nullable
    public static MongoException fromThrowable(@Nullable final Throwable t) {
        if (t == null) {
            return null;
        } else {
            return fromThrowableNonNull(t);
        }
    }

    /**
     * Static helper to create or cast a MongoException from a throwable
     *
     * @param t a throwable, which may not be null
     * @return a MongoException
     * @since 3.7
     */
    public static MongoException fromThrowableNonNull(final Throwable t) {
      if (t instanceof MongoException) {
            return (MongoException) t;
        } else {
            return new MongoException(t.getMessage(), t);
        }
    }

    /**
     * @param msg the message
     */
    public MongoException(final String msg) {
        super(msg);
        code = -3;
    }

    /**
     * @param code the error code
     * @param msg  the message
     */
    public MongoException(final int code, final String msg) {
        super(msg);
        this.code = code;
    }

    /**
     * @param msg the message
     * @param t   the throwable cause
     */
    public MongoException(final String msg, final Throwable t) {
        super(msg, t);
        code = -4;
    }

    /**
     * @param code the error code
     * @param msg  the message
     * @param t    the throwable cause
     */
    public MongoException(final int code, final String msg, final Throwable t) {
        super(msg, t);
        this.code = code;
    }

    /**
     * Gets the exception code
     *
     * @return the error code.
     */
    public int getCode() {
        return code;
    }
}

@@ -654,16 +654,153 @@
 lable;%0A%0A
+import java.util.Collections;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0Aimport static com.mongodb.assertions.Assertions.notNull;%0A%0A
 /**%0A * T
@@ -952,16 +952,291 @@
 ption %7B%0A
+%0A    /**%0A     * An error label indicating that the exception can be treated as a transient transaction error.%0A     *%0A     * @see #hasErrorLabel(String)%0A     * @since 3.8%0A     */%0A    public static final String TRANSIENT_TRANSACTION_ERROR_LABEL = %22TransientTransactionError%22;%0A%0A
     priv
@@ -1327,16 +1327,83 @@
 nt code;
+%0A    private final Set%3CString%3E errorLabels = new HashSet%3CString%3E();
 %0A%0A    /*
@@ -3162,11 +3162,1034 @@
 e;%0A    %7D
+%0A%0A    /**%0A     * Adds the given error label to the exception.%0A     *%0A     * @param errorLabel the non-null error label to add to the exception%0A     *%0A     * @since 3.8%0A     */%0A    public void addLabel(final String errorLabel) %7B%0A        notNull(%22errorLabel%22, errorLabel);%0A        errorLabels.add(errorLabel);%0A    %7D%0A%0A    /**%0A     * Gets the set of error labels associated with this exception.%0A     *%0A     * @return the error labels, which may not be null but may be empty%0A     * @since 3.8%0A     */%0A    public Set%3CString%3E getErrorLabels() %7B%0A        return Collections.unmodifiableSet(errorLabels);%0A    %7D%0A%0A    /**%0A     * Return true if the exception is labelled with the given error label, and false otherwise.%0A     *%0A     * @param errorLabel the non-null error label%0A     * @return true if the exception is labelled with the given error label%0A     * @since 3.8%0A     */%0A    public boolean hasErrorLabel(final String errorLabel) %7B%0A        notNull(%22errorLabel%22, errorLabel);%0A        return errorLabels.contains(errorLabel);%0A    %7D
 %0A%7D%0A

Add error label support to MongoException