PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms 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. @class
  9. An error, used to represent an error state.
  10. Many API's within SproutCore will return an instance of this object whenever
  11. they have an error occur. An error includes an error code, description,
  12. and optional human readable label that indicates the item that failed.
  13. Depending on the error, other properties may also be added to the object
  14. to help you recover from the failure.
  15. You can pass error objects to various UI elements to display the error in
  16. the interface. You can easily determine if the value returned by some API is
  17. an error or not using the helper SC.ok(value).
  18. h2. Faking Error Objects
  19. You can actually make any object you want to be treated like an Error object
  20. by simply implementing two properties: isError and errorValue. If you
  21. set isError to YES, then calling SC.ok(obj) on your object will return NO.
  22. If isError is YES, then SC.val(obj) will return your errorValue property
  23. instead of the receiver.
  24. @extends SC.Object
  25. @since SproutCore 1.0
  26. */
  27. SC.Error = SC.Object.extend(
  28. /** @scope SC.Error.prototype */ {
  29. /**
  30. error code. Used to designate the error type.
  31. @property {Number}
  32. */
  33. code: -1,
  34. /**
  35. Human readable description of the error. This can also be a non-localized
  36. key.
  37. @property {String}
  38. */
  39. message: '',
  40. /**
  41. The value the error represents. This is used when wrapping a value inside
  42. of an error to represent the validation failure.
  43. @property {Object}
  44. */
  45. errorValue: null,
  46. /**
  47. The original error object. Normally this will return the receiver.
  48. However, sometimes another object will masquarade as an error; this gives
  49. you a way to get at the underyling error.
  50. @property {SC.Error}
  51. */
  52. errorObject: function() {
  53. return this;
  54. }.property().cacheable(),
  55. /**
  56. Human readable name of the item with the error.
  57. @property {String}
  58. */
  59. label: null,
  60. /** @private */
  61. toString: function() {
  62. return "SC.Error:%@:%@ (%@)".fmt(SC.guidFor(this), this.get('message'), this.get('code'));
  63. },
  64. /**
  65. Walk like a duck.
  66. @property {Boolean}
  67. */
  68. isError: YES
  69. }) ;
  70. /**
  71. Creates a new SC.Error instance with the passed description, label, and
  72. code. All parameters are optional.
  73. @param description {String} human readable description of the error
  74. @param label {String} human readable name of the item with the error
  75. @param code {Number} an error code to use for testing.
  76. @returns {SC.Error} new error instance.
  77. */
  78. SC.Error.desc = function(description, label, value, code) {
  79. var opts = { message: description } ;
  80. if (label !== undefined) opts.label = label ;
  81. if (code !== undefined) opts.code = code ;
  82. if (value !== undefined) opts.errorValue = value ;
  83. return this.create(opts) ;
  84. } ;
  85. /**
  86. Shorthand form of the SC.Error.desc method.
  87. @param description {String} human readable description of the error
  88. @param label {String} human readable name of the item with the error
  89. @param code {Number} an error code to use for testing.
  90. @returns {SC.Error} new error instance.
  91. */
  92. SC.$error = function(description, label, value, c) {
  93. return SC.Error.desc(description,label, value, c);
  94. } ;
  95. /**
  96. Returns YES if the passed value is an error object or false.
  97. @param {Object} ret object value
  98. @returns {Boolean}
  99. */
  100. SC.ok = function(ret) {
  101. return (ret !== false) && !(ret && ret.isError);
  102. };
  103. /** @private */
  104. SC.$ok = SC.ok;
  105. /**
  106. Returns the value of an object. If the passed object is an error, returns
  107. the value associated with the error; otherwise returns the receiver itself.
  108. @param {Object} obj the object
  109. @returns {Object} value
  110. */
  111. SC.val = function(obj) {
  112. if (obj && obj.isError) {
  113. return obj.get ? obj.get('errorValue') : null ; // Error has no value
  114. } else return obj ;
  115. };
  116. /** @private */
  117. SC.$val = SC.val;
  118. // STANDARD ERROR OBJECTS
  119. /**
  120. Standard error code for errors that do not support multiple values.
  121. @property {Number}
  122. */
  123. SC.Error.HAS_MULTIPLE_VALUES = -100 ;
  124. ; if ((typeof SC !== 'undefined') && SC && SC.scriptDidLoad) SC.scriptDidLoad('sproutcore/runtime');