org.fastmongo.odm.mapping.exception.MappingException.java Source code

Java tutorial

Introduction

Here is the source code for org.fastmongo.odm.mapping.exception.MappingException.java

Source

/*
 * Copyright (c) 2014 Alexander Gulko <kirhog at gmail dot com>.
 *
 * 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 org.fastmongo.odm.mapping.exception;

import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.DefaultDBDecoder;

/**
 * Base mapping exception.
 *
 * @author Alexander Gulko
 */
public class MappingException extends RuntimeException {
    /**
     * A {@link com.mongodb.DBObject} byte stream on which this exception occurred.
     */
    private byte[] data;

    /**
     * A {@link com.mongodb.DBObject} on which this exception occurred.
     */
    private DBObject dbObject;

    public MappingException(String message, Throwable cause) {
        super(message, cause);
    }

    public MappingException(String message) {
        super(message);
    }

    public MappingException(String message, byte[] data) {
        super(message);
        this.data = data;
    }

    public MappingException(String message, DBObject dbObject) {
        super(message);
        this.dbObject = dbObject;
    }

    /**
     * Call this method if you need additional details.
     * <p/>
     * <strong>May take some time if DBObject needed to be decoded from byte stream.</strong>
     *
     * @return {@link com.mongodb.DBObject} on which this exception occurred.
     */
    public DBObject getDbObject() {
        if (dbObject == null && data != null) {
            dbObject = decode();
        }

        return dbObject;
    }

    private DBObject decode() {
        return new DefaultDBDecoder().decode(data, (DBCollection) null);
    }
}