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, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.runtime.common.evaluator.task; 020 021import org.apache.reef.driver.task.TaskConfigurationOptions; 022import org.apache.reef.tang.Configuration; 023import org.apache.reef.tang.Tang; 024import org.apache.reef.tang.exceptions.InjectionException; 025 026/** 027 * Thrown by REEF's resourcemanager code when it catches an exception thrown by user code. 028 */ 029public final class TaskClientCodeException extends Exception { 030 031 private final String taskId; 032 private final String contextId; 033 034 /** 035 * @param taskId the id of the failed task. 036 * @param contextId the ID of the context the failed Task was executing in. 037 * @param message the error message. 038 * @param cause the exception that caused the Task to fail. 039 */ 040 public TaskClientCodeException(final String taskId, 041 final String contextId, 042 final String message, 043 final Throwable cause) { 044 super("Failure in task '" + taskId + "' in context '" + contextId + "': " + message, cause); 045 this.taskId = taskId; 046 this.contextId = contextId; 047 } 048 049 /** 050 * Extracts a task id from the given configuration. 051 * 052 * @param config 053 * @return the task id in the given configuration. 054 * @throws RuntimeException if the configuration can't be parsed. 055 */ 056 public static String getTaskId(final Configuration config) { 057 try { 058 return Tang.Factory.getTang().newInjector(config).getNamedInstance(TaskConfigurationOptions.Identifier.class); 059 } catch (final InjectionException ex) { 060 throw new RuntimeException("Unable to determine task identifier. Giving up.", ex); 061 } 062 } 063 064 /** 065 * @return the ID of the failed Task. 066 */ 067 public String getTaskId() { 068 return this.taskId; 069 } 070 071 /** 072 * @return the ID of the context the failed Task was executing in. 073 */ 074 public String getContextId() { 075 return this.contextId; 076 } 077}