001/*
002 * Copyright (c) 2016-2017 Chris K Wensel <chris@wensel.net>. All Rights Reserved.
003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
004 *
005 * Project and contact information: http://www.cascading.org/
006 *
007 * This file is part of the Cascading project.
008 *
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package cascading.tap;
023
024import cascading.CascadingException;
025import cascading.tuple.Fields;
026import cascading.tuple.Tuple;
027import cascading.util.TraceUtil;
028
029/**
030 * Class TapException is thrown from {@link Tap} and {@link cascading.scheme.Scheme} subclasses.
031 * <p>
032 * Use the payload {@link Tuple} constructor if being thrown from inside a Scheme and which for specific data
033 * to be trapped by a failure trap Tap. If the payload is not null, and there is a trap covering the source or sink
034 * Tap in question, it will be written to the trap Tap.
035 */
036public class TapException extends CascadingException
037  {
038  Tuple payload;
039
040  /** Constructor TapException creates a new TapException instance. */
041  public TapException()
042    {
043    }
044
045  /**
046   * Constructor TapException creates a new TapException instance.
047   *
048   * @param string of type String
049   */
050  public TapException( String string )
051    {
052    super( string );
053    }
054
055  /**
056   * Constructor TapException creates a new TapException instance.
057   *
058   * @param string    of type String
059   * @param throwable of type Throwable
060   */
061  public TapException( String string, Throwable throwable )
062    {
063    super( string, throwable );
064    }
065
066  /**
067   * Constructor TapException creates a new TapException instance.
068   *
069   * @param string    of type String
070   * @param throwable of type Throwable
071   * @param payload   of type Tuple
072   */
073  public TapException( String string, Throwable throwable, Tuple payload )
074    {
075    super( string, throwable );
076    this.payload = payload;
077    }
078
079  /**
080   * Constructor TapException creates a new TapException instance.
081   *
082   * @param string  of type String
083   * @param payload of type Tuple
084   */
085  public TapException( String string, Tuple payload )
086    {
087    super( string );
088    this.payload = payload;
089    }
090
091  /**
092   * Constructor TapException creates a new TapException instance.
093   *
094   * @param throwable of type Throwable
095   */
096  public TapException( Throwable throwable )
097    {
098    super( throwable );
099    }
100
101  /**
102   * Constructor TapException creates a new TapException instance.
103   *
104   * @param tap            of type Tap
105   * @param incomingFields of type Fields
106   * @param selectorFields of type Fields
107   * @param throwable      of type Throwable
108   */
109  public TapException( Tap tap, Fields incomingFields, Fields selectorFields, Throwable throwable )
110    {
111    super( createMessage( tap, incomingFields, selectorFields ), throwable );
112    }
113
114  public Tuple getPayload()
115    {
116    return payload;
117    }
118
119  private static String createMessage( Tap tap, Fields incomingFields, Fields selectorFields )
120    {
121    String message = "unable to resolve scheme sink selector: " + selectorFields.printVerbose() +
122      ", with incoming: " + incomingFields.printVerbose();
123
124    return TraceUtil.formatTrace( tap.getScheme(), message );
125    }
126  }