package org.folio.rest.persist;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import io.vertx.pgclient.PgException;

/**
 * Easy access to
 * {@link io.vertx.pgclient.PgException PgException}
 * fields.
 */
public class PgExceptionFacade {
  private final Map<Character,String> fields;

  /**
   * @param throwable a GenericDatabaseException; any other Throwable is handled gracefully
   */
  public PgExceptionFacade(Throwable throwable) {
    if (!(throwable instanceof PgException)) {
      fields = Collections.emptyMap();
      return;
    }

    fields = new HashMap<>();
    fields.put('M', ((PgException) throwable).getMessage());
    fields.put('D', ((PgException) throwable).getDetail());
    fields.put('C', ((PgException) throwable).getCode());
  }

  /**
   * @return the fields of the underlying GenericDatabaseException, or an empty map if the throwable
   * isn't a GenericDatabaseException.
   */
  public Map<Character,String> getFields() {
    return fields;
  }

  /**
   * The SQL State code (PostgreSQL Error Codes) from the 'C' field
   * @see <a href="https://www.postgresql.org/docs/current/static/errcodes-appendix.html">PostgreSQL Error Codes</a>
   * @return state code, or empty String if not available or unknown
   */
  public String getSqlState() {
    return fields.getOrDefault('C', "");
  }

  /**
   * @return the error detail ('D' field), or empty String if not available or unknown
   */
  public String getDetail() {
    return fields.getOrDefault('D', "");
  }

  /**
   * @return the error message ('M' field), or empty String if not available or unknown
   */
  public String getMessage() {
    return fields.getOrDefault('M', "");
  }

  /**
   * @return whether the sql state is "23503 foreign_key_violation"
   */
  public boolean isForeignKeyViolation() {
    return PgExceptionUtil.FOREIGN_KEY_VIOLATION.equals(getSqlState());
  }

  /**
   * @return whether the sql state is "23505 unique_violation"
   */
  public boolean isUniqueViolation() {
    return PgExceptionUtil.UNIQUE_VIOLATION.equals(getSqlState());
  }

  /**
   * @return whether the sql state is "22P02 invalid_text_representation"
   */
  public boolean isInvalidTextRepresentation() {
    return PgExceptionUtil.INVALID_TEXT_REPRESENTATION.equals(getSqlState());
  }
}

@@ -77,76 +77,11 @@
 til.
-HashMap;%0Aimport java.util.Map;%0A%0Aimport io.vertx.pgclient.PgException
+Map
 ;%0A%0A/
@@ -216,14 +216,8 @@
 ate 
-final 
 Map%3C
@@ -405,319 +405,135 @@
     
-if (!(throwable instanceof PgException)) %7B%0A      fields = Collections.emptyMap();%0A      return;%0A    %7D%0A%0A    fields = new HashMap%3C%3E();%0A    fields.put('M', ((PgException) throwable).getMessage()
+fields = PgExceptionUtil.getBadRequestFields(throwable
 );%0A    
+if (
 fields
-.put('D', ((PgException) throwable).getDetail());%0A    fields.put('C', ((PgException) throwable).getCode());
+ == null) %7B%0A      fields = Collections.emptyMap();%0A    %7D
 %0A  %7D

Use PgExceptionUtil.getBadRequestFields