1   /*
2    * Copyright 2020 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.common;
17  
18  import static java.util.Objects.requireNonNull;
19  
20  import javax.annotation.Nullable;
21  
22  /**
23   * A {@link CentralDogmaException} that is raised when a client is attempting to send requests more than
24   * quota limits.
25   */
26  public class TooManyRequestsException extends CentralDogmaException {
27  
28      private static final long serialVersionUID = 1712601138432866984L;
29  
30      @Nullable
31      private String type;
32  
33      /**
34       * Creates a new instance.
35       */
36      public TooManyRequestsException() {}
37  
38      /**
39       * Creates a new instance.
40       */
41      public TooManyRequestsException(String type, String path, double permitsPerSecond) {
42          this('\'' + path + "' (quota limit: " + permitsPerSecond + "/sec)");
43          this.type = requireNonNull(type, "type");
44      }
45  
46      /**
47       * Creates a new instance.
48       */
49      public TooManyRequestsException(String message) {
50          super(message);
51      }
52  
53      /**
54       * Creates a new instance.
55       */
56      public TooManyRequestsException(Throwable cause) {
57          super(cause);
58      }
59  
60      /**
61       * Creates a new instance.
62       */
63      public TooManyRequestsException(String message, Throwable cause) {
64          super(message, cause);
65      }
66  
67      /**
68       * Creates a new instance.
69       *
70       * @param message the detail message
71       * @param writableStackTrace whether or not the stack trace should be writable
72       */
73      public TooManyRequestsException(String message, boolean writableStackTrace) {
74          super(message, writableStackTrace);
75      }
76  
77      /**
78       * Creates a new instance.
79       */
80      protected TooManyRequestsException(String message, Throwable cause, boolean enableSuppression,
81                                         boolean writableStackTrace) {
82          super(message, cause, enableSuppression, writableStackTrace);
83      }
84  
85      /**
86       * Returns the {@code type} specified when creating this {@link Exception}.
87       */
88      @Nullable
89      public String type() {
90          return type;
91      }
92  }
93