JSONModelError.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // JSONModelError.m
  3. //
  4. // @version 1.0.2
  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 "JSONModelError.h"
  16. NSString* const JSONModelErrorDomain = @"JSONModelErrorDomain";
  17. NSString* const kJSONModelMissingKeys = @"kJSONModelMissingKeys";
  18. NSString* const kJSONModelTypeMismatch = @"kJSONModelTypeMismatch";
  19. NSString* const kJSONModelKeyPath = @"kJSONModelKeyPath";
  20. @implementation JSONModelError
  21. +(id)errorInvalidDataWithMessage:(NSString*)message
  22. {
  23. message = [NSString stringWithFormat:@"Invalid JSON data: %@", message];
  24. return [JSONModelError errorWithDomain:JSONModelErrorDomain
  25. code:kJSONModelErrorInvalidData
  26. userInfo:@{NSLocalizedDescriptionKey:message}];
  27. }
  28. +(id)errorInvalidDataWithMissingKeys:(NSSet *)keys
  29. {
  30. return [JSONModelError errorWithDomain:JSONModelErrorDomain
  31. code:kJSONModelErrorInvalidData
  32. userInfo:@{NSLocalizedDescriptionKey:@"Invalid JSON data. Required JSON keys are missing from the input. Check the error user information.",kJSONModelMissingKeys:[keys allObjects]}];
  33. }
  34. +(id)errorInvalidDataWithTypeMismatch:(NSString*)mismatchDescription
  35. {
  36. return [JSONModelError errorWithDomain:JSONModelErrorDomain
  37. code:kJSONModelErrorInvalidData
  38. userInfo:@{NSLocalizedDescriptionKey:@"Invalid JSON data. The JSON type mismatches the expected type. Check the error user information.",kJSONModelTypeMismatch:mismatchDescription}];
  39. }
  40. +(id)errorBadResponse
  41. {
  42. return [JSONModelError errorWithDomain:JSONModelErrorDomain
  43. code:kJSONModelErrorBadResponse
  44. userInfo:@{NSLocalizedDescriptionKey:@"Bad network response. Probably the JSON URL is unreachable."}];
  45. }
  46. +(id)errorBadJSON
  47. {
  48. return [JSONModelError errorWithDomain:JSONModelErrorDomain
  49. code:kJSONModelErrorBadJSON
  50. userInfo:@{NSLocalizedDescriptionKey:@"Malformed JSON. Check the JSONModel data input."}];
  51. }
  52. +(id)errorModelIsInvalid
  53. {
  54. return [JSONModelError errorWithDomain:JSONModelErrorDomain
  55. code:kJSONModelErrorModelIsInvalid
  56. userInfo:@{NSLocalizedDescriptionKey:@"Model does not validate. The custom validation for the input data failed."}];
  57. }
  58. +(id)errorInputIsNil
  59. {
  60. return [JSONModelError errorWithDomain:JSONModelErrorDomain
  61. code:kJSONModelErrorNilInput
  62. userInfo:@{NSLocalizedDescriptionKey:@"Initializing model with nil input object."}];
  63. }
  64. - (instancetype)errorByPrependingKeyPathComponent:(NSString*)component
  65. {
  66. // Create a mutable copy of the user info so that we can add to it and update it
  67. NSMutableDictionary* userInfo = [self.userInfo mutableCopy];
  68. // Create or update the key-path
  69. NSString* existingPath = userInfo[kJSONModelKeyPath];
  70. NSString* separator = [existingPath hasPrefix:@"["] ? @"" : @".";
  71. NSString* updatedPath = (existingPath == nil) ? component : [component stringByAppendingFormat:@"%@%@", separator, existingPath];
  72. userInfo[kJSONModelKeyPath] = updatedPath;
  73. // Create the new error
  74. return [JSONModelError errorWithDomain:self.domain
  75. code:self.code
  76. userInfo:[NSDictionary dictionaryWithDictionary:userInfo]];
  77. }
  78. @end