PageRenderTime 61ms CodeModel.GetById 51ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/todos/tmp/debug/build/static/sproutcore/runtime/en/current/source/system/error.js

https://bitbucket.org/msheshtawy/bespin-login-page
JavaScript | 158 lines | 36 code | 16 blank | 106 comment | 11 complexity | 7194cc9d3ec4cd9a6c174bcf272e7aef MD5 | raw file
  1// ==========================================================================
  2// Project:   SproutCore - JavaScript Application Framework
  3// Copyright: Š2006-2009 Sprout Systems, Inc. and contributors.
  4//            Portions Š2008-2009 Apple Inc. All rights reserved.
  5// License:   Licened under MIT license (see license.js)
  6// ==========================================================================
  7
  8/**
  9  @class
 10  
 11  An error, used to represent an error state.
 12  
 13  Many API's within SproutCore will return an instance of this object whenever
 14  they have an error occur.  An error includes an error code, description,
 15  and optional human readable label that indicates the item that failed. 
 16  
 17  Depending on the error, other properties may also be added to the object
 18  to help you recover from the failure.
 19  
 20  You can pass error objects to various UI elements to display the error in
 21  the interface. You can easily determine if the value returned by some API is 
 22  an error or not using the helper SC.ok(value).
 23  
 24  h2. Faking Error Objects
 25  
 26  You can actually make any object you want to be treated like an Error object
 27  by simply implementing two properties: isError and errorValue.  If you 
 28  set isError to YES, then calling SC.ok(obj) on your object will return NO.
 29  If isError is YES, then SC.val(obj) will return your errorValue property 
 30  instead of the receiver.
 31  
 32  @extends SC.Object
 33  @since SproutCore 1.0
 34*/
 35SC.Error = SC.Object.extend(
 36/** @scope SC.Error.prototype */ {
 37  
 38  /**
 39    error code.  Used to designate the error type.
 40    
 41    @property {Number}
 42  */
 43  code: -1,
 44  
 45  /**
 46    Human readable description of the error.  This can also be a non-localized
 47    key.
 48    
 49    @property {String}
 50  */
 51  message: '',
 52  
 53  /**
 54    The value the error represents.  This is used when wrapping a value inside
 55    of an error to represent the validation failure.
 56    
 57    @property {Object}
 58  */
 59  errorValue: null,
 60  
 61  /**
 62    The original error object.  Normally this will return the receiver.  
 63    However, sometimes another object will masquarade as an error; this gives
 64    you a way to get at the underyling error.
 65    
 66    @property {SC.Error}
 67  */
 68  errorObject: function() {
 69    return this;
 70  }.property().cacheable(),
 71  
 72  /**
 73    Human readable name of the item with the error.
 74    
 75    @property {String}
 76  */
 77  label: null,
 78
 79  /** @private */
 80  toString: function() {
 81    return "SC.Error:%@:%@ (%@)".fmt(SC.guidFor(this), this.get('message'), this.get('code'));
 82  },
 83  
 84  /**
 85    Walk like a duck.
 86    
 87    @property {Boolean}
 88  */
 89  isError: YES
 90}) ;
 91
 92/**
 93  Creates a new SC.Error instance with the passed description, label, and
 94  code.  All parameters are optional.
 95  
 96  @param description {String} human readable description of the error
 97  @param label {String} human readable name of the item with the error
 98  @param code {Number} an error code to use for testing.
 99  @returns {SC.Error} new error instance.
100*/
101SC.Error.desc = function(description, label, value, code) {
102  var opts = { message: description } ;
103  if (label !== undefined) opts.label = label ;
104  if (code !== undefined) opts.code = code ;
105  if (value !== undefined) opts.errorValue = value ;
106  return this.create(opts) ;
107} ;
108
109/**
110  Shorthand form of the SC.Error.desc method.
111
112  @param description {String} human readable description of the error
113  @param label {String} human readable name of the item with the error
114  @param code {Number} an error code to use for testing.
115  @returns {SC.Error} new error instance.
116*/
117SC.$error = function(description, label, value, c) { 
118  return SC.Error.desc(description,label, value, c); 
119} ;
120
121/**
122  Returns YES if the passed value is an error object or false.
123  
124  @param {Object} ret object value
125  @returns {Boolean}
126*/
127SC.ok = function(ret) {
128  return (ret !== false) && !(ret && ret.isError);
129};
130
131/** @private */
132SC.$ok = SC.ok;
133
134/**
135  Returns the value of an object.  If the passed object is an error, returns
136  the value associated with the error; otherwise returns the receiver itself.
137  
138  @param {Object} obj the object
139  @returns {Object} value 
140*/
141SC.val = function(obj) {
142  if (obj && obj.isError) {
143    return obj.get ? obj.get('errorValue') : null ; // Error has no value
144  } else return obj ;
145};
146
147/** @private */
148SC.$val = SC.val;
149
150// STANDARD ERROR OBJECTS
151
152/**
153  Standard error code for errors that do not support multiple values.
154  
155  @property {Number}
156*/
157SC.Error.HAS_MULTIPLE_VALUES = -100 ;
158; if ((typeof SC !== 'undefined') && SC && SC.scriptDidLoad) SC.scriptDidLoad('sproutcore/runtime');