0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

自分メモ 例外処理

Posted at

認証エラーとデータベースダウンの例外処理を分けたい場合、PersistenceException にラップされる SQLException のエラーメッセージやエラーコードを解析して適切に分類する方法があります。

1. PersistenceException にラップされた SQLException を取得

まず、PersistenceExceptiongetCause() を使用して SQLException を取得し、getSQLState()getErrorCode() を利用してエラーを分類できます。


2. 具体的な処理

try {
    SqlSession session = sqlSessionFactory.openSession();
} catch (PersistenceException e) {
    Throwable cause = e.getCause();
    
    if (cause instanceof SQLException) {
        SQLException sqlEx = (SQLException) cause;
        String sqlState = sqlEx.getSQLState();
        int errorCode = sqlEx.getErrorCode();
        
        if (isAuthenticationError(sqlState, errorCode)) {
            log.error("認証エラー: {}", sqlEx.getMessage(), sqlEx);
            throw new AuthenticationException("認証エラーが発生しました", sqlEx);
        } else if (isDatabaseDownError(sqlState, errorCode)) {
            log.error("データベースダウン: {}", sqlEx.getMessage(), sqlEx);
            throw new DatabaseDownException("データベースが利用できません", sqlEx);
        } else {
            log.error("データベース接続エラー: {}", sqlEx.getMessage(), sqlEx);
            throw e; // その他のエラーは再スロー
        }
    } else {
        log.error("PersistenceException: {}", e.getMessage(), e);
        throw e; // 予期しないエラーはそのまま再スロー
    }
}

3. エラー分類の関数

(1) 認証エラーの判定

private boolean isAuthenticationError(String sqlState, int errorCode) {
    return "28000".equals(sqlState) // SQLSTATE 28000 は一般的な認証エラー
        || errorCode == 1045 // MySQL: Access denied for user
        || errorCode == 18456; // SQL Server: Login failed
}

(2) データベースダウンエラーの判定

private boolean isDatabaseDownError(String sqlState, int errorCode) {
    return "08S01".equals(sqlState) // SQLSTATE 08S01: 通信障害
        || errorCode == 2003 // MySQL: Can't connect to MySQL server
        || errorCode == 0 // 一般的な接続不可 (JDBCドライバによる)
        || errorCode == 57P01; // PostgreSQL: database shutdown
}

4. カスタム例外クラス(任意)

もしエラーを明確に区別してスローしたい場合、独自の例外クラスを作成するのも良い方法です。

(1) 認証エラー用の例外

public class AuthenticationException extends RuntimeException {
    public AuthenticationException(String message, Throwable cause) {
        super(message, cause);
    }
}

(2) データベースダウンエラー用の例外

public class DatabaseDownException extends RuntimeException {
    public DatabaseDownException(String message, Throwable cause) {
        super(message, cause);
    }
}

5. まとめ

PersistenceException にラップされた SQLException を解析することで、エラーを分類できる。
SQLState とエラーコードを使って「認証エラー」「データベースダウンエラー」を判定する。
必要なら AuthenticationExceptionDatabaseDownException を定義してスローする。

この方法で、認証エラーとデータベースダウンエラーを適切に分けた例外処理ができます!

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?