Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / OperationResult.java
blobf9756d097f4e260cb7dd9e182c7f4844035283b8
1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.search;
5 import com.google.appengine.api.search.SearchServicePb.SearchServiceError.ErrorCode;
6 import com.google.apphosting.api.ApiProxy;
8 import java.io.Serializable;
10 /**
11 * The result of an operation involving the search service.
14 public class OperationResult implements Serializable {
15 private static final long serialVersionUID = 3608247775865189592L;
17 private final StatusCode code;
18 private final String message;
20 public OperationResult(SearchServicePb.RequestStatus status) {
21 this(status.getCode(), status.hasErrorDetail() ? status.getErrorDetail() : null);
24 /**
25 * @param code the status code of the request extracted from proto buffers
26 * @param errorDetail detailed error message or {@code null}
28 public OperationResult(ErrorCode code, String errorDetail) {
29 this(StatusCode.fromErrorCode(code), errorDetail);
32 /**
33 * @param code the status code of the request
34 * @param errorDetail detailed error message or {@code null}
36 public OperationResult(StatusCode code, String errorDetail) {
37 this.code = code;
38 this.message = errorDetail;
41 /**
42 * @return the status code
44 public StatusCode getCode() {
45 return code;
48 /**
49 * @return the detailed message or {@code null}
51 public String getMessage() {
52 return message;
55 @Override
56 public String toString() {
57 if (message == null) {
58 return code.name();
60 return code.name() + ": " + message;
63 @Override
64 public boolean equals(Object object) {
65 if (object == this) {
66 return true;
68 if (!getClass().isAssignableFrom(object.getClass())) {
69 return false;
71 OperationResult result = (OperationResult) object;
72 boolean sameMessages = (message == null && result.message == null)
73 || (message != null && message.equals(result.message));
74 return code.equals(result.code) && sameMessages;
77 /**
78 * Converts a {@Throwable} into an OperationResult if it is an
79 * {@link ApiProxy.ApplicationException} with an error which is not OK. If
80 * the error is not known, the OperationResult will default to INTERNAL_ERROR.
82 static OperationResult convertToOperationResult(Throwable cause) {
83 if (cause instanceof ApiProxy.ApplicationException) {
84 ApiProxy.ApplicationException exception = ((ApiProxy.ApplicationException) cause);
85 int applicationError = exception.getApplicationError();
86 if (applicationError != 0) {
87 ErrorCode code = ErrorCode.valueOf(applicationError);
88 if (code == null) {
89 return new OperationResult(StatusCode.INTERNAL_ERROR, exception.getErrorDetail());
90 } else {
91 return new OperationResult(StatusCode.fromErrorCode(code), exception.getErrorDetail());
95 return null;