Skip to content

Commit 9f7f180

Browse files
committed
Adding getLastError(), getPreviousError(), resetError()
and forceError() for testing
1 parent 71961a5 commit 9f7f180

4 files changed

Lines changed: 162 additions & 0 deletions

File tree

examples/QuickTour.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,43 @@ public static void main(String[] args) throws Exception {
158158
for (DBObject o : list) {
159159
System.out.println(o);
160160
}
161+
162+
/*
163+
* See if the last operation had an error
164+
*/
165+
System.out.println("Last error : " + db.getLastError());
166+
167+
/*
168+
* see if any previous operation had an error
169+
*/
170+
System.out.println("Previous error : " + db.getPreviousError());
171+
172+
/*
173+
* force an error
174+
*/
175+
176+
db.forceError();
177+
178+
/*
179+
* See if the last operation had an error
180+
*/
181+
System.out.println("Last error : " + db.getLastError());
182+
183+
/*
184+
* see if any previous operation had an error
185+
*/
186+
System.out.println("Previous error : " + db.getPreviousError());
187+
188+
/*
189+
* See if the last operation had an error
190+
*/
191+
System.out.println("Last error : " + db.getLastError());
192+
193+
db.resetError();
194+
195+
/*
196+
* see if any previous operation had an error
197+
*/
198+
System.out.println("Previous error : " + db.getPreviousError());
161199
}
162200
}

src/main/com/mongodb/DBApiLayer.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,55 @@ public boolean authenticate(String username, String passwd) {
9191
return res.getInt("ok") == 1;
9292
}
9393

94+
/**
95+
* Gets the the error (if there is one) from the previous operation. The result of
96+
* this command will look like
97+
*
98+
* <pre>
99+
* { "err" : errorMessage , "ok" : 1.0 , "_ns" : "$cmd"}
100+
* </pre>
101+
*
102+
* The value for errorMessage will be null if no error occurred, or a description otherwise.
103+
*
104+
* @return DBObject with error and status information
105+
*/
106+
public DBObject getLastError() {
107+
return command(new BasicDBObject("getlasterror", 1));
108+
}
109+
110+
/**
111+
* Returns the last error that occurred since start of database or a call to <code>resetError()</code>
112+
*
113+
* The return object will look like
114+
*
115+
* <pre>
116+
* { err : errorMessage, nPrev : countOpsBack, ok : 1 }
117+
* </pre>
118+
*
119+
* The value for errormMessage will be null of no error has ocurred, or the message. The value of
120+
* countOpsBack will be the number of operations since the error occurred.
121+
*
122+
* @return DBObject with error and status information
123+
*/
124+
public DBObject getPreviousError() {
125+
return command(new BasicDBObject("getpreverror", 1));
126+
}
127+
128+
/**
129+
* Resets the error memory for this database. Used to clear all errors such that getPreviousError()
130+
* will return no error.
131+
*/
132+
public void resetError() {
133+
command(new BasicDBObject("reseterror", 1));
134+
}
135+
136+
/**
137+
* For testing purposes only - this method forces an error to help test error handling
138+
*/
139+
public void forceError() {
140+
command(new BasicDBObject("forceerror", 1));
141+
}
142+
94143
/**
95144
* Drops this database. Removes all data on disk. Use with caution.
96145
*/
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* See the NOTICE.txt file distributed with this work for
3+
* information regarding copyright ownership.
4+
*
5+
* The authors license this file to you under the
6+
* Apache License, Version 2.0 (the "License"); you may not use
7+
* this file except in compliance with the License. You may
8+
* obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package com.mongodb;
20+
21+
import org.testng.annotations.Test;
22+
import org.testng.annotations.BeforeClass;
23+
24+
/**
25+
*
26+
*/
27+
public class ErrorTest {
28+
29+
Mongo _db;
30+
31+
@BeforeClass
32+
public void setUp() throws Exception{
33+
_db = new Mongo("com_mongodb_ErrorTest");
34+
}
35+
36+
@Test
37+
public void testLastError() {
38+
39+
assert(_db.getLastError().get("err") == null);
40+
41+
_db.forceError();
42+
43+
assert(_db.getLastError().get("err") != null);
44+
45+
_db.resetError();
46+
assert(_db.getLastError().get("err") == null);
47+
}
48+
49+
@Test
50+
public void testPrevError() {
51+
52+
_db.resetError();
53+
54+
assert(_db.getLastError().get("err") == null);
55+
assert(_db.getPreviousError().get("err") == null);
56+
57+
_db.forceError();
58+
59+
assert(_db.getLastError().get("err") != null);
60+
assert(_db.getPreviousError().get("err") != null);
61+
62+
_db.getCollection("misc").insert(new BasicDBObject("foo", 1));
63+
64+
assert(_db.getLastError().get("err") == null);
65+
assert(_db.getPreviousError().get("err") != null);
66+
67+
_db.resetError();
68+
69+
assert(_db.getLastError().get("err") == null);
70+
assert(_db.getPreviousError().get("err") == null);
71+
}
72+
73+
}

testng.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<class name="com.mongodb.ReflectionTest" />
1313
<class name="com.mongodb.DBAddressTest" />
1414

15+
<class name="com.mongodb.ErrorTest" />
16+
1517
</classes>
1618
</test>
1719

0 commit comments

Comments
 (0)