package com.orientechnologies.common.exception;

import com.orientechnologies.common.util.OApi;
import com.orientechnologies.orient.core.exception.OBackupInProgressException;
import com.orientechnologies.orient.core.exception.OQueryParsingException;

import java.lang.reflect.InvocationTargetException;

/**
 * Enumeration with the error managed by OrientDB. This class has been introduced in v.2.2 and little by little will contain all the
 * OrientDB managed errors.
 * 
 * @author Luigi Dell'Aquila
 */
@OApi(maturity = OApi.MATURITY.NEW)
public enum OErrorCode {

  // eg.
  QUERY_PARSE_ERROR(OErrorCategory.SQL_PARSING, 1, "query parse error", OQueryParsingException.class), BACKUP_IN_PROGRESS(
      OErrorCategory.STORAGE, 2, "You are trying to start a backup, but it is already in progress",
      OBackupInProgressException.class);

  protected final OErrorCategory              category;
  protected final int                         code;
  protected final String                      description;
  protected final Class<? extends OException> exceptionClass;

  OErrorCode(OErrorCategory category, int code, String description) {
    this(category, code, description, OException.class);
  }

  OErrorCode(OErrorCategory category, int code, String description, Class<? extends OException> exceptionClass) {
    this.category = category;
    this.code = code;
    this.description = description;
    this.exceptionClass = exceptionClass;
  }

  public int getCode() {
    return code;
  }

  public void throwException() {
    throwException(this.description, null);
  }

  public void throwException(String message) {
    throwException(message, null);
  }

  public void throwException(Throwable parent) {
    throwException(this.description, parent);
  }

  public void throwException(String message, Throwable parent) {
    final String fullMessage = String.format("%1$06d_%2$06d - %s", category.code, code, message);
    try {
      throw exceptionClass.getConstructor(String.class, Exception.class).newInstance(fullMessage, parent);
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }

  }

}

@@ -38,24 +38,77 @@
 exception;%0A%0A
+import com.orientechnologies.common.log.OLogManager;%0A
 import com.o
@@ -1998,14 +1998,50 @@
     
-throw 
+OException exc = OException.wrapException(
 exce
@@ -2082,25 +2082,8 @@
 lass
-, Exception.class
 ).ne
@@ -2092,27 +2092,24 @@
 nstance(
-fullM
+m
 essage
+)
 , parent
@@ -2111,16 +2111,33 @@
 arent);%0A
+      throw exc;%0A
     %7D ca
@@ -2179,215 +2179,699 @@
     
-e.printStackTrace();%0A    %7D catch (IllegalAccessException e) %7B%0A      e.printStackTrace();%0A    %7D catch (NoSuchMethodException e) %7B%0A      e.printStackTrace();%0A    %7D catch (InvocationTargetException e) %7B%0A      e
+OLogManager.instance().warn(this, %22Cannot instantiate exception %22+exceptionClass);%0A      e.printStackTrace();%0A      parent.printStackTrace();%0A    %7D catch (IllegalAccessException e) %7B%0A      OLogManager.instance().warn(this, %22Cannot instantiate exception %22+exceptionClass);%0A      e.printStackTrace();%0A      parent.printStackTrace();%0A    %7D catch (NoSuchMethodException e) %7B%0A      OLogManager.instance().warn(this, %22Cannot instantiate exception %22+exceptionClass);%0A      e.printStackTrace();%0A      parent.printStackTrace();%0A    %7D catch (InvocationTargetException e) %7B%0A      OLogManager.instance().warn(this, %22Cannot instantiate exception %22+exceptionClass);%0A      e.printStackTrace();%0A      parent
 .pri

Fix OErrorCode exception handling to use Exception.wrapException()