|
25 | 25 | public class CommandResult extends BasicDBObject {
|
26 | 26 |
|
27 | 27 | CommandResult(ServerAddress srv) {
|
28 |
| - this(null, srv); |
29 |
| - } |
30 |
| - |
31 |
| - CommandResult(DBObject cmd, ServerAddress srv) { |
32 | 28 | if (srv == null) {
|
33 | 29 | throw new IllegalArgumentException("server address is null");
|
34 | 30 | }
|
35 |
| - _cmd = cmd; |
36 | 31 | _host = srv;
|
37 | 32 | //so it is shown in toString/debug
|
38 | 33 | put("serverUsed", srv.toString());
|
@@ -61,55 +56,36 @@ public boolean ok(){
|
61 | 56 | * @return The error message or null
|
62 | 57 | */
|
63 | 58 | public String getErrorMessage(){
|
64 |
| - Object foo = get( "errmsg" ); |
65 |
| - if ( foo == null ) |
| 59 | + Object errorMessage = get( "errmsg" ); |
| 60 | + if ( errorMessage == null ) |
66 | 61 | return null;
|
67 |
| - return foo.toString(); |
| 62 | + return errorMessage.toString(); |
68 | 63 | }
|
69 | 64 |
|
70 | 65 | /**
|
71 | 66 | * utility method to create an exception with the command name
|
72 | 67 | * @return The mongo exception or null
|
73 | 68 | */
|
74 |
| - public MongoException getException(){ |
75 |
| - if ( !ok() ) { |
76 |
| - StringBuilder buf = new StringBuilder(); |
77 |
| - |
78 |
| - String cmdName; |
79 |
| - if (_cmd != null) { |
80 |
| - cmdName = _cmd.keySet().iterator().next(); |
81 |
| - buf.append( "command failed [" ).append( cmdName ).append( "]: " ); |
82 |
| - } else { |
83 |
| - buf.append( "operation failed: "); |
| 69 | + public MongoException getException() { |
| 70 | + if ( !ok() ) { // check for command failure |
| 71 | + return new CommandFailureException( this ); |
| 72 | + } else if ( hasErr() ) { // check for errors reported by getlasterror command |
| 73 | + if (getCode() == 11000 || getCode() == 11001 || getCode() == 12582) { |
| 74 | + return new MongoException.DuplicateKey(this); |
84 | 75 | }
|
85 |
| - |
86 |
| - buf.append( toString() ); |
87 |
| - |
88 |
| - return new CommandFailure( this , buf.toString() ); |
89 |
| - } else { |
90 |
| - // GLE check |
91 |
| - if ( hasErr() ) { |
92 |
| - Object foo = get( "err" ); |
93 |
| - |
94 |
| - int code = getCode(); |
95 |
| - |
96 |
| - String s = foo.toString(); |
97 |
| - if ( code == 11000 || code == 11001 || s.startsWith( "E11000" ) || s.startsWith( "E11001" ) ) |
98 |
| - return new MongoException.DuplicateKey( code , s ); |
99 |
| - |
100 |
| - return new MongoException( code , s ); |
| 76 | + else { |
| 77 | + return new MongoException.WriteConcernException(this); |
101 | 78 | }
|
102 | 79 | }
|
103 | 80 |
|
104 |
| - //all good, should never get here. |
105 |
| - return null; |
| 81 | + throw new IllegalStateException("This method should not be called if there is no exception"); |
106 | 82 | }
|
107 | 83 |
|
108 | 84 | /**
|
109 | 85 | * returns the "code" field, as an int
|
110 | 86 | * @return -1 if there is no code
|
111 | 87 | */
|
112 |
| - int getCode(){ |
| 88 | + int getCode() { |
113 | 89 | int code = -1;
|
114 | 90 | if ( get( "code" ) instanceof Number )
|
115 | 91 | code = ((Number)get("code")).intValue();
|
@@ -139,20 +115,24 @@ public ServerAddress getServerUsed() {
|
139 | 115 | return _host;
|
140 | 116 | }
|
141 | 117 |
|
142 |
| - private final DBObject _cmd; |
143 | 118 | private final ServerAddress _host;
|
144 | 119 | private static final long serialVersionUID = 1L;
|
145 | 120 |
|
146 |
| - static class CommandFailure extends MongoException { |
| 121 | + static class CommandFailureException extends MongoException { |
147 | 122 | private static final long serialVersionUID = 1L;
|
| 123 | + private final CommandResult commandResult; |
148 | 124 |
|
149 | 125 | /**
|
150 | 126 | *
|
151 |
| - * @param res the result |
152 |
| - * @param msg the message |
| 127 | + * @param commandResult the result |
153 | 128 | */
|
154 |
| - public CommandFailure( CommandResult res , String msg ){ |
155 |
| - super( ServerError.getCode( res ) , msg ); |
| 129 | + public CommandFailureException(CommandResult commandResult){ |
| 130 | + super(ServerError.getCode(commandResult), commandResult.toString()); |
| 131 | + this.commandResult = commandResult; |
| 132 | + } |
| 133 | + |
| 134 | + public CommandResult getCommandResult() { |
| 135 | + return commandResult; |
156 | 136 | }
|
157 | 137 | }
|
158 | 138 | }
|
0 commit comments