1  /**
     2   * com.sekati.except.Catcher
     3   * @version 1.1.1
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8  
     9  import com.sekati.except.Exception;
    10  import com.sekati.log.Logger;
    11  import com.sekati.reflect.Stringifier;
    12  import com.sekati.validate.TypeValidation;
    13  
    14  /**
    15   * Generic {@link Exception} catch handler.
    16   * {@code Usage:
    17   * 	function test(isOk:Boolean):Void {
    18   * 		try {
    19   * 			throw new Exception(this,"An unknown Error has occurred",arguments);
    20   * 		} catch (e:Exception) {
    21   * 			Catcher.handle(e);
    22   * 		}
    23   * 	}
    24   * }
    25   */
    26  class com.sekati.except.Catcher {
    27  
    28  	/**
    29  	 * A generic Exception handler that returns string data about the Exception for logging or tracing.
    30  	 * @param e (Exception)
    31  	 * @return String
    32  	 */
    33  	public static function handle(e:Exception):String {
    34  		var a:Array = e.getStack( );
    35  		var tmp:String = "[ ";
    36  		for(var i:Number = 0; i < a.length ; i++) tmp += a[i] + " (" + TypeValidation.getType( a[i] ).name + "), ";	
    37  		var stack:String = tmp.slice( 0, tmp.length - 2 ) + " ]";
    38  		var str:String = "%%% Catcher handling Exception:\nName: '" + e.getName( ) + "'\nType: '" + e.getType( ) + "'\nErrorCode: '" + e.getErrorCode( ) + "'\nThrower: " + e.getThrower( ) + "\nStack: " + stack + "\n\n";
    39  		Logger.$.warn( toString( ), str );	
    40  		return str;
    41  	}
    42  
    43  	/**
    44  	 * Simple reflection for Static classes.
    45  	 */
    46  	public static function toString():String {
    47  		return Stringifier.stringify( Catcher );
    48  	}
    49  
    50  	private function Catcher() {
    51  	}
    52  }