Clean up error code related classes

This commit is contained in:
김선우
2025-01-17 03:56:26 +09:00
parent fcadc2f825
commit 7e78ec448b
7 changed files with 163 additions and 32 deletions

View File

@@ -1,8 +1,46 @@
package org.github.tursodatabase;
import org.github.tursodatabase.core.SqliteCode;
/**
* Limbo error code. Superset of SQLite3 error code.
*/
public enum LimboErrorCode {
SQLITE_OK(SqliteCode.SQLITE_OK, "Successful result"),
SQLITE_ERROR(SqliteCode.SQLITE_ERROR, "SQL error or missing database"),
SQLITE_INTERNAL(SqliteCode.SQLITE_INTERNAL, "An internal logic error in SQLite"),
SQLITE_PERM(SqliteCode.SQLITE_PERM, "Access permission denied"),
SQLITE_ABORT(SqliteCode.SQLITE_ABORT, "Callback routine requested an abort"),
SQLITE_BUSY(SqliteCode.SQLITE_BUSY, "The database file is locked"),
SQLITE_LOCKED(SqliteCode.SQLITE_LOCKED, "A table in the database is locked"),
SQLITE_NOMEM(SqliteCode.SQLITE_NOMEM, "A malloc() failed"),
SQLITE_READONLY(SqliteCode.SQLITE_READONLY, "Attempt to write a readonly database"),
SQLITE_INTERRUPT(SqliteCode.SQLITE_INTERRUPT, "Operation terminated by sqlite_interrupt()"),
SQLITE_IOERR(SqliteCode.SQLITE_IOERR, "Some kind of disk I/O error occurred"),
SQLITE_CORRUPT(SqliteCode.SQLITE_CORRUPT, "The database disk image is malformed"),
SQLITE_NOTFOUND(SqliteCode.SQLITE_NOTFOUND, "(Internal Only) Table or record not found"),
SQLITE_FULL(SqliteCode.SQLITE_FULL, "Insertion failed because database is full"),
SQLITE_CANTOPEN(SqliteCode.SQLITE_CANTOPEN, "Unable to open the database file"),
SQLITE_PROTOCOL(SqliteCode.SQLITE_PROTOCOL, "Database lock protocol error"),
SQLITE_EMPTY(SqliteCode.SQLITE_EMPTY, "(Internal Only) Database table is empty"),
SQLITE_SCHEMA(SqliteCode.SQLITE_SCHEMA, "The database schema changed"),
SQLITE_TOOBIG(SqliteCode.SQLITE_TOOBIG, "Too much data for one row of a table"),
SQLITE_CONSTRAINT(SqliteCode.SQLITE_CONSTRAINT, "Abort due to constraint violation"),
SQLITE_MISMATCH(SqliteCode.SQLITE_MISMATCH, "Data type mismatch"),
SQLITE_MISUSE(SqliteCode.SQLITE_MISUSE, "Library used incorrectly"),
SQLITE_NOLFS(SqliteCode.SQLITE_NOLFS, "Uses OS features not supported on host"),
SQLITE_AUTH(SqliteCode.SQLITE_AUTH, "Authorization denied"),
SQLITE_ROW(SqliteCode.SQLITE_ROW, "sqlite_step() has another row ready"),
SQLITE_DONE(SqliteCode.SQLITE_DONE, "sqlite_step() has finished executing"),
SQLITE_INTEGER(SqliteCode.SQLITE_INTEGER, "Integer type"),
SQLITE_FLOAT(SqliteCode.SQLITE_FLOAT, "Float type"),
SQLITE_TEXT(SqliteCode.SQLITE_TEXT, "Text type"),
SQLITE_BLOB(SqliteCode.SQLITE_BLOB, "Blob type"),
SQLITE_NULL(SqliteCode.SQLITE_NULL, "Null type"),
UNKNOWN_ERROR(-1, "Unknown error"),
ETC(9999, "Unclassified error");
LIMBO_DATABASE_ALREADY_CLOSED(1000, "Database already closed"),
LIMBO_ETC(9999, "Unclassified error");
public final int code;
public final String message;

View File

@@ -15,7 +15,10 @@
*/
package org.github.tursodatabase.core;
public class Codes {
/**
* Sqlite error codes.
*/
public class SqliteCode {
/** Successful result */
public static final int SQLITE_OK = 0;

View File

@@ -1,13 +0,0 @@
package org.github.tursodatabase.exceptions;
/**
* This class defines error codes that correspond to specific error conditions
* that may occur while communicating with the JNI.
* <p />
* Refer to ErrorCode in rust package.
* TODO: Deprecate
*/
public class ErrorCode {
public static int CONNECTION_FAILURE = -1;
}

View File

@@ -0,0 +1,40 @@
package org.github.tursodatabase.utils;
import org.github.tursodatabase.LimboErrorCode;
import org.github.tursodatabase.annotations.Nullable;
import org.github.tursodatabase.exceptions.LimboException;
import java.sql.SQLException;
import static org.github.tursodatabase.utils.ByteArrayUtils.utf8ByteBufferToString;
public class LimboExceptionUtils {
/**
* Throws formatted SQLException with error code and message.
*
* @param errorCode Error code.
* @param errorMessageBytes Error message.
*/
public static void throwLimboException(int errorCode, byte[] errorMessageBytes) throws SQLException {
String errorMessage = utf8ByteBufferToString(errorMessageBytes);
throw buildLimboException(errorCode, errorMessage);
}
/**
* Throws formatted SQLException with error code and message.
*
* @param errorCode Error code.
* @param errorMessage Error message.
*/
public static LimboException buildLimboException(int errorCode, @Nullable String errorMessage) throws SQLException {
LimboErrorCode code = LimboErrorCode.getErrorCode(errorCode);
String msg;
if (code == LimboErrorCode.UNKNOWN_ERROR) {
msg = String.format("%s:%s (%s)", code, errorCode, errorMessage);
} else {
msg = String.format("%s (%s)", code, errorMessage);
}
return new LimboException(msg, code);
}
}

View File

@@ -1,5 +1,6 @@
package org.github.tursodatabase;
import org.github.tursodatabase.core.LimboConnection;
import org.junit.jupiter.api.Test;
import java.sql.Connection;

View File

@@ -36,7 +36,7 @@ public class LimboDBTest {
LimboDB.load();
LimboDB db = LimboDB.create("jdbc:sqlite:" + dbPath, dbPath);
final int limboExceptionCode = LimboErrorCode.ETC.code;
final int limboExceptionCode = LimboErrorCode.LIMBO_ETC.code;
try {
db.throwJavaException(limboExceptionCode);
} catch (Exception e) {