View Javadoc

1   /*
2    * Copyright (C) 2010 Atlassian
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   */
16  
17  package com.atlassian.jira.rest.client;
18  
19  import com.google.common.base.Joiner;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.Collections;
25  
26  /**
27   * Basic exception which may be thrown by any remote operation encapsulated by the REST client.
28   * Usually some more specific exception will be chained here and available via {@link #getCause()}
29   *
30   * @since v0.1
31   */
32  public class RestClientException extends RuntimeException {
33      private final Collection<String> errorMessages;
34  
35      public RestClientException(Throwable cause) {
36          super(cause);
37          errorMessages = Collections.emptyList();
38      }
39      public RestClientException(String errorMessage, Throwable cause) {
40          super(errorMessage, cause);
41          this.errorMessages = Arrays.asList(errorMessage);
42      }
43  
44  
45      /**
46       * @param errorMessages messages which will be joined with newline character and accessible then via {@link #getMessage()}
47       * @param cause the cause of this exception or <code>null</code>
48       */
49      public RestClientException(Collection<String> errorMessages, Throwable cause) {
50          super(Joiner.on("\n").join(errorMessages), cause);
51          this.errorMessages = new ArrayList<String>(errorMessages);
52      }
53  
54      /**
55       * @param errorMessages messages which will be joined with newline character and accessible then via {@link #getMessage()}
56       */
57      public RestClientException(Collection<String> errorMessages) {
58          super(Joiner.on("\n").join(errorMessages));
59          this.errorMessages = new ArrayList<String>(errorMessages);
60      }
61  
62      /**
63       * @return error messages used while building this exception object
64       */
65      public Iterable<String> getErrorMessages() {
66          return errorMessages;
67      }
68  }