Permalink
Browse files

forgot to add file

  • Loading branch information...
1 parent 18dca77 commit eb208c762472c3e0af05f95bf9b1a8cfa36016c6 @erh erh committed May 24, 2010
Showing with 54 additions and 0 deletions.
  1. +54 −0 src/main/com/mongodb/ServerError.java
@@ -0,0 +1,54 @@
+// ServerError.java
+
+package com.mongodb;
+
+import org.bson.*;
+
+public class ServerError {
+
+ ServerError( DBObject o ){
+ Object foo = o.get( "$err" );
+ if ( foo == null )
+ throw new IllegalArgumentException( "need to have $err" );
+
+ _err = foo.toString();
+ _code = _getCode( o );
+ }
+
+ static int _getCode( BSONObject o ){
+ Object c = o.get( "code" );
+ if ( c == null )
+ c = o.get( "$code" );
+
+ if ( c == null )
+ return -5;
+
+ return ((Number)c).intValue();
+ }
+
+ public String getError(){
+ return _err;
+ }
+
+ public int getCode(){
+ return _code;
+ }
+
+ public boolean isNotMasterError(){
+ return
+ _err.equals( "not master" ) ||
+ _code == 10054 ||
+ _code == 10056 ||
+ _code == 10058 ||
+ _code == 10107;
+ }
+
+ public String toString(){
+ if ( _code > 0 )
+ return _code + " " + _err;
+ return _err;
+ }
+
+ final String _err;
+ final int _code;
+}

0 comments on commit eb208c7

Please sign in to comment.