App Engine Python SDK version 1.9.8
[gae.git] / java / src / main / com / google / appengine / api / search / RequestStatusUtil.java
blob615c2bd76b457db4f5acec42f0b655f847d5d2f6
1 package com.google.appengine.api.search;
3 import com.google.apphosting.api.AppEngineInternal;
4 import com.google.common.base.Preconditions;
5 import com.google.common.collect.ImmutableMap;
6 import com.google.net.util.error.Codes.Code;
8 /**
9 * Collection of utility methods for SearchServicePb.RequestStatus.
11 @AppEngineInternal
12 public final class RequestStatusUtil {
14 /**
15 * Mapping of search service error to general Canonical Errors.
17 private static final ImmutableMap<SearchServicePb.SearchServiceError.ErrorCode, Code>
18 REQUEST_STATUS_TO_CANONICAL_ERROR_MAPPING =
19 ImmutableMap.<SearchServicePb.SearchServiceError.ErrorCode, Code>builder()
20 .put(SearchServicePb.SearchServiceError.ErrorCode.OK, Code.OK)
21 .put(SearchServicePb.SearchServiceError.ErrorCode.INVALID_REQUEST, Code.INVALID_ARGUMENT)
22 .put(SearchServicePb.SearchServiceError.ErrorCode.TRANSIENT_ERROR, Code.UNAVAILABLE)
23 .put(SearchServicePb.SearchServiceError.ErrorCode.INTERNAL_ERROR, Code.INTERNAL)
24 .put(SearchServicePb.SearchServiceError.ErrorCode.PERMISSION_DENIED, Code.PERMISSION_DENIED)
25 .put(SearchServicePb.SearchServiceError.ErrorCode.TIMEOUT, Code.DEADLINE_EXCEEDED)
26 .put(SearchServicePb.SearchServiceError.ErrorCode.CONCURRENT_TRANSACTION, Code.ABORTED)
27 .build();
29 /**
30 * Converts SearchServicePb.SearchServiceError.ErrorCode to canonical error code.
32 public static Code toCanonicalCode(SearchServicePb.SearchServiceError.ErrorCode appCode) {
33 return Preconditions.checkNotNull(REQUEST_STATUS_TO_CANONICAL_ERROR_MAPPING.get(appCode));
36 /**
37 * Creates a SearchServicePb.RequestStatus.Builder from the given code and message.
39 public static SearchServicePb.RequestStatus.Builder newStatusBuilder(
40 SearchServicePb.SearchServiceError.ErrorCode code, String message) {
41 SearchServicePb.RequestStatus.Builder builder = SearchServicePb.RequestStatus.newBuilder();
42 builder.setCode(code).setCanonicalCode(toCanonicalCode(code).getNumber());
43 if (message != null) {
44 builder.setErrorDetail(message);
46 return builder;
49 /**
50 * Creates a SearchServicePb.RequestStatus from the given code and message.
52 public static SearchServicePb.RequestStatus newStatus(
53 SearchServicePb.SearchServiceError.ErrorCode code, String message) {
54 return newStatusBuilder(code, message).build();
57 /**
58 * Creates a SearchServicePb.RequestStatus from the given code.
60 public static SearchServicePb.RequestStatus newStatus(
61 SearchServicePb.SearchServiceError.ErrorCode code) {
62 return newStatusBuilder(code, null).build();
65 /**
66 * Creates a RequestStatus message suitable for reporting an invalid request.
68 public static SearchServicePb.RequestStatus newInvalidRequestStatus(IllegalArgumentException e) {
69 Preconditions.checkNotNull(e.getMessage());
70 return newStatus(SearchServicePb.SearchServiceError.ErrorCode.INVALID_REQUEST, e.getMessage());
73 /**
74 * Creates a RequestStatus message suitable for reporting an unknown index. We use
75 * {@link SearchServicePb.SearchServiceError.ErrorCode#OK} because the unknown index isn't
76 * an error condition but just a notice to the user.
78 public static SearchServicePb.RequestStatus newUnknownIndexStatus(
79 SearchServicePb.IndexSpec indexSpec) {
80 return newStatus(SearchServicePb.SearchServiceError.ErrorCode.OK, String.format(
81 "Index '%s' in namespace '%s' does not exist",
82 indexSpec.getName(), indexSpec.getNamespace()));