JSONModelError.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // JSONModelError.h
  3. //
  4. // @version 1.0.0
  5. // @author Marin Todorov, http://www.touch-code-magazine.com
  6. //
  7. // Copyright (c) 2012-2014 Marin Todorov, Underplot ltd.
  8. // This code is distributed under the terms and conditions of the MIT license.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  11. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  13. //
  14. // The MIT License in plain English: http://www.touch-code-magazine.com/JSONModel/MITLicense
  15. #import <Foundation/Foundation.h>
  16. /////////////////////////////////////////////////////////////////////////////////////////////
  17. typedef NS_ENUM(int, kJSONModelErrorTypes)
  18. {
  19. kJSONModelErrorInvalidData = 1,
  20. kJSONModelErrorBadResponse = 2,
  21. kJSONModelErrorBadJSON = 3,
  22. kJSONModelErrorModelIsInvalid = 4,
  23. kJSONModelErrorNilInput = 5
  24. };
  25. /////////////////////////////////////////////////////////////////////////////////////////////
  26. /** The domain name used for the JSONModelError instances */
  27. extern NSString* const JSONModelErrorDomain;
  28. /**
  29. * If the model JSON input misses keys that are required, check the
  30. * userInfo dictionary of the JSONModelError instance you get back -
  31. * under the kJSONModelMissingKeys key you will find a list of the
  32. * names of the missing keys.
  33. */
  34. extern NSString* const kJSONModelMissingKeys;
  35. /**
  36. * If JSON input has a different type than expected by the model, check the
  37. * userInfo dictionary of the JSONModelError instance you get back -
  38. * under the kJSONModelTypeMismatch key you will find a description
  39. * of the mismatched types.
  40. */
  41. extern NSString* const kJSONModelTypeMismatch;
  42. /**
  43. * If an error occurs in a nested model, check the userInfo dictionary of
  44. * the JSONModelError instance you get back - under the kJSONModelKeyPath
  45. * key you will find key-path at which the error occurred.
  46. */
  47. extern NSString* const kJSONModelKeyPath;
  48. /////////////////////////////////////////////////////////////////////////////////////////////
  49. /**
  50. * Custom NSError subclass with shortcut methods for creating
  51. * the common JSONModel errors
  52. */
  53. @interface JSONModelError : NSError
  54. @property (strong, nonatomic) NSHTTPURLResponse* httpResponse;
  55. /**
  56. * Creates a JSONModelError instance with code kJSONModelErrorInvalidData = 1
  57. */
  58. +(id)errorInvalidDataWithMessage:(NSString*)message;
  59. /**
  60. * Creates a JSONModelError instance with code kJSONModelErrorInvalidData = 1
  61. * @param keys a set of field names that were required, but not found in the input
  62. */
  63. +(id)errorInvalidDataWithMissingKeys:(NSSet*)keys;
  64. /**
  65. * Creates a JSONModelError instance with code kJSONModelErrorInvalidData = 1
  66. * @param A description of the type mismatch that was encountered.
  67. */
  68. +(id)errorInvalidDataWithTypeMismatch:(NSString*)mismatchDescription;
  69. /**
  70. * Creates a JSONModelError instance with code kJSONModelErrorBadResponse = 2
  71. */
  72. +(id)errorBadResponse;
  73. /**
  74. * Creates a JSONModelError instance with code kJSONModelErrorBadJSON = 3
  75. */
  76. +(id)errorBadJSON;
  77. /**
  78. * Creates a JSONModelError instance with code kJSONModelErrorModelIsInvalid = 4
  79. */
  80. +(id)errorModelIsInvalid;
  81. /**
  82. * Creates a JSONModelError instance with code kJSONModelErrorNilInput = 5
  83. */
  84. +(id)errorInputIsNil;
  85. /**
  86. * Creates a new JSONModelError with the same values plus information about the key-path of the error.
  87. * Properties in the new error object are the same as those from the receiver,
  88. * except that a new key kJSONModelKeyPath is added to the userInfo dictionary.
  89. * This key contains the component string parameter. If the key is already present
  90. * then the new error object has the component string prepended to the existing value.
  91. */
  92. - (instancetype)errorByPrependingKeyPathComponent:(NSString*)component;
  93. /////////////////////////////////////////////////////////////////////////////////////////////
  94. @end