Skip to main content
summaryrefslogtreecommitdiffstats
blob: c6bde87484abe843caefa542cd9a64ffc9fdd3d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylyn.internal.github.core;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;

import org.eclipse.egit.github.core.FieldError;
import org.eclipse.egit.github.core.RequestError;
import org.eclipse.egit.github.core.client.RequestException;

/**
 * GitHub exception that wraps and formats a {@link RequestException}
 */
public class GitHubException extends IOException {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1456910662911777231L;

	/**
	 * Wraps the given {@link IOException} with a {@link GitHubException} if it
	 * is a {@link RequestException} instance.
	 * 
	 * @param exception
	 * @return wrapped exception
	 */
	public static IOException wrap(IOException exception) {
		return exception instanceof RequestException ? new GitHubException(
				(RequestException) exception) : exception;
	}

	/**
	 * Create GitHub exception from {@link RequestException}
	 * 
	 * @param cause
	 */
	public GitHubException(RequestException cause) {
		super(cause);
	}

	public String getMessage() {
		RequestError error = ((RequestException) getCause()).getError();
		String errorMessage = error.getMessage();
		if (errorMessage == null)
			errorMessage = ""; //$NON-NLS-1$
		StringBuilder message = new StringBuilder(errorMessage);
		List<FieldError> errors = error.getErrors();
		if (errors != null && errors.size() > 0) {
			message.append(':');
			for (FieldError fieldError : errors)
				message.append(' ').append(format(fieldError)).append(',');
			message.deleteCharAt(message.length() - 1);
		}
		return message.toString();
	}

	private String format(FieldError error) {
		String code = error.getCode();
		String value = error.getValue();
		String field = error.getField();
		String resource = error.getResource();
		if (FieldError.CODE_INVALID.equals(code))
			if (value != null)
				return MessageFormat
						.format(Messages.FieldError_InvalidFieldWithValue,
								value, field);
			else
				return MessageFormat.format(Messages.FieldError_InvalidField,
						field, value);
		else if (FieldError.CODE_MISSING_FIELD.equals(code))
			return MessageFormat
					.format(Messages.FieldError_MissingField, field);
		else
			return MessageFormat.format(Messages.FieldError_ResourceError,
					field, resource);
	}

}

Back to the top