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.api;
18  
19  import com.atlassian.jira.rest.client.api.domain.util.ErrorCollection;
20  import com.google.common.base.Optional;
21  import com.google.common.collect.ImmutableList;
22  
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 com.atlassian.jira.rest.client.api.
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  
34  	private final Optional<Integer> statusCode;
35  	private final Collection<ErrorCollection> errorCollections;
36  
37  	public RestClientException(final RestClientException exception) {
38  		super(exception.getMessage(), exception);
39  		this.statusCode = exception.getStatusCode();
40  		this.errorCollections = exception.errorCollections;
41  	}
42  
43  	public RestClientException(final Throwable cause) {
44  		super(cause);
45  		this.errorCollections = Collections.emptyList();
46  		this.statusCode = Optional.absent();
47  	}
48  
49  	public RestClientException(final Throwable cause, final int statusCode) {
50  		super(cause);
51  		this.errorCollections = Collections.emptyList();
52  		this.statusCode = Optional.of(statusCode);
53  	}
54  
55  	public RestClientException(final String errorMessage, final Throwable cause) {
56  		super(errorMessage, cause);
57  		this.errorCollections = ImmutableList.of(new ErrorCollection(errorMessage));
58  		statusCode = Optional.absent();
59  	}
60  
61  	public RestClientException(final Collection<ErrorCollection> errorCollections, final int statusCode) {
62  		super(errorCollections.toString());
63  		this.errorCollections = ImmutableList.copyOf(errorCollections);
64  		this.statusCode = Optional.of(statusCode);
65  	}
66  
67  	public RestClientException(final Collection<ErrorCollection> errorCollections, final Throwable cause, final int statusCode) {
68  		super(errorCollections.toString(), cause);
69  		this.errorCollections = ImmutableList.copyOf(errorCollections);
70  		this.statusCode = Optional.of(statusCode);
71  	}
72  
73  	/**
74  	 * @return error messages used while building this exception object
75  	 */
76  	public Collection<ErrorCollection> getErrorCollections() {
77  		return errorCollections;
78  	}
79  
80  	/**
81  	 * @return optional error code of failed http request.
82  	 */
83  	public Optional<Integer> getStatusCode() {
84  		return statusCode;
85  	}
86  
87  	@Override
88  	public String toString() {
89  		return "RestClientException{" +
90  				"statusCode=" + statusCode +
91  				", errorCollections=" + errorCollections +
92  				'}';
93  	}
94  }