View Javadoc
1   /*
2    * Copyright (C) 2008, Google Inc.
3    * Copyright (C) 2008, Jonas Fonseca <fonseca@diku.dk>
4    * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
5    * Copyright (C) 2006-2007, Shawn O. Pearce <spearce@spearce.org> and others
6    *
7    * This program and the accompanying materials are made available under the
8    * terms of the Eclipse Distribution License v. 1.0 which is available at
9    * https://www.eclipse.org/org/documents/edl-v10.php.
10   *
11   * SPDX-License-Identifier: BSD-3-Clause
12   */
13  
14  package org.eclipse.jgit.errors;
15  
16  import java.io.IOException;
17  import java.text.MessageFormat;
18  
19  import org.eclipse.jgit.annotations.Nullable;
20  import org.eclipse.jgit.internal.JGitText;
21  import org.eclipse.jgit.lib.AnyObjectId;
22  import org.eclipse.jgit.lib.ObjectChecker;
23  import org.eclipse.jgit.lib.ObjectId;
24  
25  /**
26   * Exception thrown when an object cannot be read from Git.
27   */
28  public class CorruptObjectException extends IOException {
29  	private static final long serialVersionUID = 1L;
30  
31  	private ObjectChecker.ErrorType errorType;
32  
33  	/**
34  	 * Report a specific error condition discovered in an object.
35  	 *
36  	 * @param type
37  	 *            type of error
38  	 * @param id
39  	 *            identity of the bad object
40  	 * @param why
41  	 *            description of the error.
42  	 * @since 4.2
43  	 */
44  	public CorruptObjectException(ObjectChecker.ErrorType type, AnyObjectId id,
45  			String why) {
46  		super(MessageFormat.format(JGitText.get().objectIsCorrupt3,
47  				type.getMessageId(), id.name(), why));
48  		this.errorType = type;
49  	}
50  
51  	/**
52  	 * Construct a CorruptObjectException for reporting a problem specified
53  	 * object id
54  	 *
55  	 * @param id
56  	 *            a {@link org.eclipse.jgit.lib.AnyObjectId}
57  	 * @param why
58  	 *            error message
59  	 */
60  	public CorruptObjectException(AnyObjectId id, String why) {
61  		super(MessageFormat.format(JGitText.get().objectIsCorrupt, id.name(), why));
62  	}
63  
64  	/**
65  	 * Construct a CorruptObjectException for reporting a problem specified
66  	 * object id
67  	 *
68  	 * @param id
69  	 *            a {@link org.eclipse.jgit.lib.ObjectId}
70  	 * @param why
71  	 *            error message
72  	 */
73  	public CorruptObjectException(ObjectId id, String why) {
74  		super(MessageFormat.format(JGitText.get().objectIsCorrupt, id.name(), why));
75  	}
76  
77  	/**
78  	 * Construct a CorruptObjectException for reporting a problem not associated
79  	 * with a specific object id.
80  	 *
81  	 * @param why
82  	 *            error message
83  	 */
84  	public CorruptObjectException(String why) {
85  		super(why);
86  	}
87  
88  	/**
89  	 * Construct a CorruptObjectException for reporting a problem not associated
90  	 * with a specific object id.
91  	 *
92  	 * @param why
93  	 *            message describing the corruption.
94  	 * @param cause
95  	 *            optional root cause exception
96  	 * @since 3.4
97  	 */
98  	public CorruptObjectException(String why, Throwable cause) {
99  		super(why);
100 		initCause(cause);
101 	}
102 
103 	/**
104 	 * Specific error condition identified by
105 	 * {@link org.eclipse.jgit.lib.ObjectChecker}.
106 	 *
107 	 * @return error condition or null.
108 	 * @since 4.2
109 	 */
110 	@Nullable
111 	public ObjectChecker.ErrorType getErrorType() {
112 		return errorType;
113 	}
114 }