1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.providers.downloads;
17
18import static android.provider.Downloads.Impl.STATUS_UNHANDLED_HTTP_CODE;
19import static android.provider.Downloads.Impl.STATUS_UNHANDLED_REDIRECT;
20
21/**
22 * Raised to indicate that the current request should be stopped immediately.
23 *
24 * Note the message passed to this exception will be logged and therefore must be guaranteed
25 * not to contain any PII, meaning it generally can't include any information about the request
26 * URI, headers, or destination filename.
27 */
28class StopRequestException extends Exception {
29    private final int mFinalStatus;
30
31    public StopRequestException(int finalStatus, String message) {
32        super(message);
33        mFinalStatus = finalStatus;
34    }
35
36    public StopRequestException(int finalStatus, Throwable t) {
37        this(finalStatus, t.getMessage());
38        initCause(t);
39    }
40
41    public StopRequestException(int finalStatus, String message, Throwable t) {
42        this(finalStatus, message);
43        initCause(t);
44    }
45
46    public int getFinalStatus() {
47        return mFinalStatus;
48    }
49
50    public static StopRequestException throwUnhandledHttpError(int code, String message)
51            throws StopRequestException {
52        final String error = "Unhandled HTTP response: " + code + " " + message;
53        if (code >= 400 && code < 600) {
54            throw new StopRequestException(code, error);
55        } else if (code >= 300 && code < 400) {
56            throw new StopRequestException(STATUS_UNHANDLED_REDIRECT, error);
57        } else {
58            throw new StopRequestException(STATUS_UNHANDLED_HTTP_CODE, error);
59        }
60    }
61}
62