Skip to content

Commit 231c25f

Browse files
committed
Add Meta.error
1 parent e0dbc51 commit 231c25f

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/main/java/org/lmdbjava/Library.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public MDB_envinfo(jnr.ffi.Runtime runtime) {
8585

8686
public interface Lmdb {
8787

88+
/**
89+
* Return the error description for this result code.
90+
*/
91+
String mdb_strerror(int rc);
92+
8893
Pointer mdb_version(IntByReference major, IntByReference minor, IntByReference patch);
8994

9095
/**

src/main/java/org/lmdbjava/Meta.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323
*/
2424
public final class Meta {
2525

26+
/**
27+
* Fetches the LMDB error code description.
28+
* <p>
29+
* End users should not need this method, as LmdbJava converts all LMDB
30+
* exceptions into a typed Java exception that incorporates the error code.
31+
* However it is provided here for verification and troubleshooting (eg if the
32+
* user wishes to see the original LMDB description of the error code, or
33+
* there is a newer library version etc).
34+
*
35+
* @param err the error code returned from LMDB
36+
* @return the description
37+
*/
38+
public static String error(final int err) {
39+
return lib.mdb_strerror(err);
40+
}
41+
2642
/**
2743
* Obtains the LMDB C library version information.
2844
*

src/test/java/org/lmdbjava/MetaTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import static org.hamcrest.CoreMatchers.nullValue;
66
import static org.hamcrest.MatcherAssert.assertThat;
77
import org.junit.Test;
8+
import static org.lmdbjava.LmdbNativeException.PageCorruptedException.MDB_CORRUPTED;
89
import org.lmdbjava.Meta.Version;
10+
import static org.lmdbjava.Meta.error;
911
import static org.lmdbjava.TestUtils.invokePrivateConstructor;
1012

1113
public class MetaTest {
@@ -15,11 +17,18 @@ public void coverPrivateConstructors() throws Exception {
1517
invokePrivateConstructor(Meta.class);
1618
}
1719

20+
@Test
21+
public void errCode() {
22+
assertThat(error(MDB_CORRUPTED), is(
23+
"MDB_CORRUPTED: Located page was wrong type"));
24+
}
25+
1826
@Test
1927
public void version() throws Exception {
2028
final Version v = Meta.version();
2129
assertThat(v, not(nullValue()));
2230
assertThat(v.major, is(0));
2331
assertThat(v.minor, is(9));
2432
}
33+
2534
}

0 commit comments

Comments
 (0)