001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.hadoop.fs;
019
020 import java.io.IOException;
021
022 /**
023 * Exceptions based on standard posix/linux style exceptions for path related
024 * errors. Returns an exception with the format "path: standard error string".
025 *
026 * This exception corresponds to Error Input/ouput(EIO)
027 */
028 public class PathIOException extends IOException {
029 static final long serialVersionUID = 0L;
030 private static final String EIO = "Input/output error";
031 // NOTE: this really should be a Path, but a Path is buggy and won't
032 // return the exact string used to construct the path, and it mangles
033 // uris with no authority
034 private String operation;
035 private String path;
036 private String targetPath;
037
038 /**
039 * Constructor a generic I/O error exception
040 * @param path for the exception
041 */
042 public PathIOException(String path) {
043 this(path, EIO, null);
044 }
045
046 /**
047 * Appends the text of a Throwable to the default error message
048 * @param path for the exception
049 * @param cause a throwable to extract the error message
050 */
051 public PathIOException(String path, Throwable cause) {
052 this(path, EIO, cause);
053 }
054
055 /**
056 * Avoid using this method. Use a subclass of PathIOException if
057 * possible.
058 * @param path for the exception
059 * @param error custom string to use an the error text
060 */
061 public PathIOException(String path, String error) {
062 this(path, error, null);
063 }
064
065 protected PathIOException(String path, String error, Throwable cause) {
066 super(error, cause);
067 this.path = path;
068 }
069
070 /** Format:
071 * cmd: {operation} `path' {to `target'}: error string
072 */
073 @Override
074 public String getMessage() {
075 StringBuilder message = new StringBuilder();
076 if (operation != null) {
077 message.append(operation + " ");
078 }
079 message.append(formatPath(path));
080 if (targetPath != null) {
081 message.append(" to " + formatPath(targetPath));
082 }
083 message.append(": " + super.getMessage());
084 if (getCause() != null) {
085 message.append(": " + getCause().getMessage());
086 }
087 return message.toString();
088 }
089
090 /** @return Path that generated the exception */
091 public Path getPath() { return new Path(path); }
092
093 /** @return Path if the operation involved copying or moving, else null */
094 public Path getTargetPath() {
095 return (targetPath != null) ? new Path(targetPath) : null;
096 }
097
098 /**
099 * Optional operation that will preface the path
100 * @param operation a string
101 */
102 public void setOperation(String operation) {
103 this.operation = operation;
104 }
105
106 /**
107 * Optional path if the exception involved two paths, ex. a copy operation
108 * @param targetPath the of the operation
109 */
110 public void setTargetPath(String targetPath) {
111 this.targetPath = targetPath;
112 }
113
114 private String formatPath(String path) {
115 return "`" + path + "'";
116 }
117 }