|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package com.google.api.gax.rpc; |
| 31 | + |
| 32 | +import com.google.common.collect.ImmutableMap; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.concurrent.CancellationException; |
| 35 | +import org.jspecify.annotations.NullMarked; |
| 36 | +import org.jspecify.annotations.Nullable; |
| 37 | + |
| 38 | +/** |
| 39 | + * Classifies exceptions encountered during resumable upload commands into protocol error |
| 40 | + * categories. |
| 41 | + */ |
| 42 | +@NullMarked |
| 43 | +final class UploadErrorClassifier { |
| 44 | + |
| 45 | + enum Category { |
| 46 | + TRANSIENT, |
| 47 | + RECOVERABLE, |
| 48 | + FATAL |
| 49 | + } |
| 50 | + |
| 51 | + private static final ImmutableMap<Integer, Category> HTTP_STATUS_MAP = |
| 52 | + ImmutableMap.<Integer, Category>builder() |
| 53 | + .put(408, Category.TRANSIENT) |
| 54 | + .put(429, Category.TRANSIENT) |
| 55 | + .put(500, Category.TRANSIENT) |
| 56 | + .put(502, Category.TRANSIENT) |
| 57 | + .put(503, Category.TRANSIENT) |
| 58 | + .put(504, Category.TRANSIENT) |
| 59 | + .put(400, Category.RECOVERABLE) |
| 60 | + .put(409, Category.RECOVERABLE) |
| 61 | + .put(412, Category.RECOVERABLE) |
| 62 | + .put(416, Category.RECOVERABLE) |
| 63 | + .put(401, Category.FATAL) |
| 64 | + .put(403, Category.FATAL) |
| 65 | + .put(404, Category.FATAL) |
| 66 | + .put(405, Category.FATAL) |
| 67 | + .put(410, Category.FATAL) |
| 68 | + .put(413, Category.FATAL) |
| 69 | + .put(415, Category.FATAL) |
| 70 | + .build(); |
| 71 | + |
| 72 | + private UploadErrorClassifier() {} |
| 73 | + |
| 74 | + /** |
| 75 | + * Classifies an exception for the given upload command according to protocol rules. |
| 76 | + * |
| 77 | + * @param t the error to classify |
| 78 | + * @param command the upload command that produced the error |
| 79 | + * @return the classified error category |
| 80 | + */ |
| 81 | + static Category classify(@Nullable Throwable t, UploadCommand command) { |
| 82 | + if (t == null) { |
| 83 | + return Category.FATAL; |
| 84 | + } |
| 85 | + |
| 86 | + // Cancellation is terminal and never retryable. |
| 87 | + if (t instanceof CancellationException) { |
| 88 | + return Category.FATAL; |
| 89 | + } |
| 90 | + |
| 91 | + // Handle ApiExceptions. |
| 92 | + if (t instanceof ApiException) { |
| 93 | + ApiException apiException = (ApiException) t; |
| 94 | + |
| 95 | + // Code.UNKNOWN indicates an unrecognised throwable where synthetic HTTP 500 |
| 96 | + // should be ignored. |
| 97 | + // Real HTTP 500 responses arrive with Code.INTERNAL. |
| 98 | + if (apiException.getStatusCode().getCode() == StatusCode.Code.UNKNOWN) { |
| 99 | + Throwable cause = apiException.getCause(); |
| 100 | + if (cause instanceof IOException) { |
| 101 | + return Category.TRANSIENT; |
| 102 | + } |
| 103 | + return Category.FATAL; |
| 104 | + } |
| 105 | + |
| 106 | + // Server rejections (final/cancelled on non-2xx) and protocol violations map to |
| 107 | + // FAILED_PRECONDITION and are terminal. HTTP 408 and 412 also map to FAILED_PRECONDITION in |
| 108 | + // HttpJsonStatusCode but have distinct protocol categories. |
| 109 | + Object transportCode = apiException.getStatusCode().getTransportCode(); |
| 110 | + if (apiException.getStatusCode().getCode() == StatusCode.Code.FAILED_PRECONDITION |
| 111 | + && !Integer.valueOf(408).equals(transportCode) |
| 112 | + && !Integer.valueOf(412).equals(transportCode)) { |
| 113 | + return Category.FATAL; |
| 114 | + } |
| 115 | + |
| 116 | + // Status table lookup on raw HTTP transport code. |
| 117 | + if (transportCode instanceof Integer) { |
| 118 | + Category category = HTTP_STATUS_MAP.get(transportCode); |
| 119 | + if (category != null) { |
| 120 | + return category; |
| 121 | + } |
| 122 | + } |
| 123 | + return Category.FATAL; |
| 124 | + } |
| 125 | + |
| 126 | + // Plain I/O or timeout exceptions that bypassed ApiException wrapping. |
| 127 | + if (t instanceof IOException) { |
| 128 | + return Category.TRANSIENT; |
| 129 | + } |
| 130 | + |
| 131 | + // Unrecognized errors fail the upload immediately. Never default to retryable. |
| 132 | + return Category.FATAL; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Classifies a missing upload status response header according to the wire command. |
| 137 | + * |
| 138 | + * @param command the upload command that received a response lacking the status header |
| 139 | + * @return the classified error category |
| 140 | + */ |
| 141 | + static Category classifyMissingStatusHeader(UploadCommand command) { |
| 142 | + switch (command) { |
| 143 | + case UPLOAD: |
| 144 | + case FINALIZE: |
| 145 | + case UPLOAD_FINALIZE: |
| 146 | + return Category.RECOVERABLE; |
| 147 | + case QUERY: |
| 148 | + case CANCEL: |
| 149 | + default: |
| 150 | + return Category.FATAL; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments