|
16 | 16 |
|
17 | 17 | package io.cdap.plugin.batch.source; |
18 | 18 |
|
| 19 | +import com.google.common.base.Throwables; |
| 20 | +import io.cdap.cdap.api.exception.ErrorCategory; |
| 21 | +import io.cdap.cdap.api.exception.ErrorType; |
| 22 | +import io.cdap.cdap.api.exception.ErrorUtils; |
| 23 | +import io.cdap.cdap.api.exception.ProgramFailureException; |
| 24 | +import io.cdap.cdap.api.metadata.MetadataException; |
| 25 | +import io.cdap.cdap.etl.api.exception.ErrorContext; |
19 | 26 | import io.cdap.plugin.common.HydratorErrorDetailsProvider; |
| 27 | +import org.apache.hadoop.fs.ChecksumException; |
| 28 | +import org.apache.hadoop.fs.FileAlreadyExistsException; |
| 29 | +import org.apache.hadoop.fs.InvalidPathException; |
| 30 | +import org.apache.hadoop.fs.InvalidRequestException; |
| 31 | +import org.apache.hadoop.fs.ParentNotDirectoryException; |
| 32 | +import org.apache.hadoop.fs.PathIsNotDirectoryException; |
| 33 | +import org.apache.hadoop.hdfs.BlockMissingException; |
| 34 | +import org.apache.hadoop.hdfs.protocol.QuotaExceededException; |
| 35 | +import org.apache.hadoop.hdfs.security.token.block.InvalidBlockTokenException; |
| 36 | +import org.apache.hadoop.hdfs.server.datanode.ReplicaNotFoundException; |
| 37 | +import org.apache.hadoop.hdfs.server.namenode.SafeModeException; |
| 38 | +import org.apache.hadoop.ipc.RemoteException; |
| 39 | +import org.apache.hadoop.ipc.StandbyException; |
| 40 | +import org.apache.hadoop.security.AccessControlException; |
| 41 | +import org.apache.hadoop.util.DiskChecker; |
| 42 | + |
| 43 | +import java.io.FileNotFoundException; |
| 44 | +import java.net.NoRouteToHostException; |
| 45 | +import java.net.SocketTimeoutException; |
| 46 | +import java.util.List; |
| 47 | +import java.util.concurrent.TimeoutException; |
| 48 | +import javax.annotation.Nullable; |
| 49 | +import javax.security.auth.login.FailedLoginException; |
20 | 50 |
|
21 | 51 | /** |
22 | 52 | * FileErrorDetails provider |
23 | 53 | */ |
24 | 54 | public class FileErrorDetailsProvider extends HydratorErrorDetailsProvider { |
25 | | - |
| 55 | + static final String ERROR_MESSAGE_FORMAT = "Error occurred in the phase: '%s'. %s: %s"; |
| 56 | + |
| 57 | + @Override |
| 58 | + public ProgramFailureException getExceptionDetails(Exception e, ErrorContext errorContext) { |
| 59 | + // Call super method to get base exception details |
| 60 | + ProgramFailureException ex = super.getExceptionDetails(e, errorContext); |
| 61 | + if (ex != null) { |
| 62 | + return ex; |
| 63 | + } |
| 64 | + |
| 65 | + // Use the new function to handle file-based program failure exceptions |
| 66 | + return getFileBasedExceptionDetails(e, errorContext); |
| 67 | + } |
| 68 | + |
| 69 | + public ProgramFailureException getFileBasedExceptionDetails(Exception e, ErrorContext errorContext) { |
| 70 | + List<Throwable> causalChain = Throwables.getCausalChain(e); |
| 71 | + for (Throwable t : causalChain) { |
| 72 | + if (t instanceof FileNotFoundException) { |
| 73 | + return getFileBasedProgramFailureException((FileNotFoundException) t, errorContext, ErrorType.USER); |
| 74 | + } |
| 75 | + if (t instanceof AccessControlException) { |
| 76 | + return getFileBasedProgramFailureException((AccessControlException) t, errorContext, ErrorType.USER); |
| 77 | + } |
| 78 | + if (t instanceof ParentNotDirectoryException) { |
| 79 | + return getFileBasedProgramFailureException((ParentNotDirectoryException) t, errorContext, ErrorType.USER); |
| 80 | + } |
| 81 | + if (t instanceof InvalidPathException) { |
| 82 | + return getFileBasedProgramFailureException((InvalidPathException) t, errorContext, ErrorType.USER); |
| 83 | + } |
| 84 | + if (t instanceof FileAlreadyExistsException) { |
| 85 | + return getFileBasedProgramFailureException((FileAlreadyExistsException) t, errorContext, ErrorType.USER); |
| 86 | + } |
| 87 | + if (t instanceof QuotaExceededException) { |
| 88 | + return getFileBasedProgramFailureException((QuotaExceededException) t, errorContext, ErrorType.USER); |
| 89 | + } |
| 90 | + if (t instanceof PathIsNotDirectoryException) { |
| 91 | + return getFileBasedProgramFailureException((PathIsNotDirectoryException) t, errorContext, ErrorType.USER); |
| 92 | + } |
| 93 | + if (t instanceof InvalidRequestException) { |
| 94 | + return getFileBasedProgramFailureException((InvalidRequestException) t, errorContext, ErrorType.USER); |
| 95 | + } |
| 96 | + if (t instanceof ChecksumException) { |
| 97 | + return getFileBasedProgramFailureException((ChecksumException) t, errorContext, ErrorType.USER); |
| 98 | + } |
| 99 | + if (t instanceof RemoteException) { |
| 100 | + return getFileBasedProgramFailureException((RemoteException) t, errorContext, ErrorType.SYSTEM); |
| 101 | + } |
| 102 | + if (t instanceof SocketTimeoutException) { |
| 103 | + return getFileBasedProgramFailureException((SocketTimeoutException) t, errorContext, ErrorType.SYSTEM); |
| 104 | + } |
| 105 | + if (t instanceof DiskChecker.DiskOutOfSpaceException) { |
| 106 | + return getFileBasedProgramFailureException((DiskChecker.DiskOutOfSpaceException) t, errorContext, ErrorType.SYSTEM); |
| 107 | + } |
| 108 | + if (t instanceof StandbyException) { |
| 109 | + return getFileBasedProgramFailureException((StandbyException) t, errorContext, ErrorType.SYSTEM); |
| 110 | + } |
| 111 | + if (t instanceof NoRouteToHostException) { |
| 112 | + return getFileBasedProgramFailureException((NoRouteToHostException) t, errorContext, ErrorType.SYSTEM); |
| 113 | + } |
| 114 | + if (t instanceof BlockMissingException) { |
| 115 | + return getFileBasedProgramFailureException((BlockMissingException) t, errorContext, ErrorType.SYSTEM); |
| 116 | + } |
| 117 | + if (t instanceof ReplicaNotFoundException) { |
| 118 | + return getFileBasedProgramFailureException((ReplicaNotFoundException) t, errorContext, ErrorType.SYSTEM); |
| 119 | + } |
| 120 | + if (t instanceof InvalidBlockTokenException) { |
| 121 | + return getFileBasedProgramFailureException((InvalidBlockTokenException) t, errorContext, ErrorType.SYSTEM); |
| 122 | + } |
| 123 | + if (t instanceof SafeModeException) { |
| 124 | + return getFileBasedProgramFailureException((SafeModeException) t, errorContext, ErrorType.SYSTEM); |
| 125 | + } |
| 126 | + if (t instanceof TimeoutException) { |
| 127 | + return getFileBasedProgramFailureException((TimeoutException) t, errorContext, ErrorType.SYSTEM); |
| 128 | + } |
| 129 | + if (t instanceof FailedLoginException) { |
| 130 | + return getFileBasedProgramFailureException((FailedLoginException) t, errorContext, ErrorType.SYSTEM); |
| 131 | + } |
| 132 | + if (t instanceof MetadataException) { |
| 133 | + return getFileBasedProgramFailureException((MetadataException) t, errorContext, ErrorType.SYSTEM); |
| 134 | + } |
| 135 | + } |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + public ProgramFailureException getFileBasedProgramFailureException(Exception e, @Nullable ErrorContext errorContext, |
| 140 | + ErrorType errorType) { |
| 141 | + String errorMessage = e.getMessage(); |
| 142 | + return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), |
| 143 | + errorMessage, errorContext != null ? String.format(ERROR_MESSAGE_FORMAT, errorContext.getPhase(), e.getClass() |
| 144 | + .getName(), errorMessage) : String.format("%s: %s", e.getClass().getName(), errorMessage), errorType, false, e); |
| 145 | + } |
26 | 146 | } |
0 commit comments