Permalink
Browse files

Extracting +mtl_modelErrorWithException:

  • Loading branch information...
robb committed Jul 6, 2013
1 parent c0593fe commit ffb0851299778b403f611797a6d199c67745e708
@@ -12,6 +12,12 @@
1ED5B5D0163A4E3C0072668E /* NSObject+MTLComparisonAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ED5B5CE163A4E3C0072668E /* NSObject+MTLComparisonAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
1ED5B5D1163A4E3C0072668E /* NSObject+MTLComparisonAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 1ED5B5CF163A4E3C0072668E /* NSObject+MTLComparisonAdditions.m */; };
1ED5B5D2163A4E3C0072668E /* NSObject+MTLComparisonAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 1ED5B5CF163A4E3C0072668E /* NSObject+MTLComparisonAdditions.m */; };
+ 54803A32178829A800011B39 /* NSError+MTLModelException.h in Headers */ = {isa = PBXBuildFile; fileRef = 54803A30178829A700011B39 /* NSError+MTLModelException.h */; };
+ 54803A33178829A800011B39 /* NSError+MTLModelException.h in Headers */ = {isa = PBXBuildFile; fileRef = 54803A30178829A700011B39 /* NSError+MTLModelException.h */; };
+ 54803A34178829A800011B39 /* NSError+MTLModelException.m in Sources */ = {isa = PBXBuildFile; fileRef = 54803A31178829A700011B39 /* NSError+MTLModelException.m */; };
+ 54803A35178829A800011B39 /* NSError+MTLModelException.m in Sources */ = {isa = PBXBuildFile; fileRef = 54803A31178829A700011B39 /* NSError+MTLModelException.m */; };
+ 54803A3B17882CCD00011B39 /* MTLErrorModelExceptionSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 54803A3A17882CCD00011B39 /* MTLErrorModelExceptionSpec.m */; };
+ 54803A3C17882CCD00011B39 /* MTLErrorModelExceptionSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 54803A3A17882CCD00011B39 /* MTLErrorModelExceptionSpec.m */; };
88080C18160A706900CCABF2 /* NSArray+MTLManipulationAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 88080C16160A706900CCABF2 /* NSArray+MTLManipulationAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
88080C19160A706900CCABF2 /* NSArray+MTLManipulationAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 88080C17160A706900CCABF2 /* NSArray+MTLManipulationAdditions.m */; };
88080C1A160A706900CCABF2 /* NSArray+MTLManipulationAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 88080C17160A706900CCABF2 /* NSArray+MTLManipulationAdditions.m */; };
@@ -207,6 +213,9 @@
1ED5B5CB163A4E2B0072668E /* MTLComparisonAdditionsSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTLComparisonAdditionsSpec.m; sourceTree = "<group>"; };
1ED5B5CE163A4E3C0072668E /* NSObject+MTLComparisonAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+MTLComparisonAdditions.h"; sourceTree = "<group>"; };
1ED5B5CF163A4E3C0072668E /* NSObject+MTLComparisonAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+MTLComparisonAdditions.m"; sourceTree = "<group>"; };
+ 54803A30178829A700011B39 /* NSError+MTLModelException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSError+MTLModelException.h"; sourceTree = "<group>"; };
+ 54803A31178829A700011B39 /* NSError+MTLModelException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSError+MTLModelException.m"; sourceTree = "<group>"; };
+ 54803A3A17882CCD00011B39 /* MTLErrorModelExceptionSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTLErrorModelExceptionSpec.m; sourceTree = "<group>"; };
88080C16160A706900CCABF2 /* NSArray+MTLManipulationAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+MTLManipulationAdditions.h"; sourceTree = "<group>"; };
88080C17160A706900CCABF2 /* NSArray+MTLManipulationAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+MTLManipulationAdditions.m"; sourceTree = "<group>"; };
88080C1C160A719D00CCABF2 /* MTLArrayManipulationSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTLArrayManipulationSpec.m; sourceTree = "<group>"; };
@@ -455,6 +464,7 @@
88080C1C160A719D00CCABF2 /* MTLArrayManipulationSpec.m */,
1ED5B5CB163A4E2B0072668E /* MTLComparisonAdditionsSpec.m */,
D0C27CF5161107E5002FE587 /* MTLDictionaryManipulationSpec.m */,
+ 54803A3A17882CCD00011B39 /* MTLErrorModelExceptionSpec.m */,
D02E48F016CB8ADB00257645 /* MTLJSONAdapterSpec.m */,
D0ABD7A01746D56E006BEA7D /* MTLManagedObjectAdapterSpec.m */,
D02E48E916CB8ACA00257645 /* MTLModelNSCodingSpec.m */,
@@ -471,6 +481,8 @@
children = (
88080C16160A706900CCABF2 /* NSArray+MTLManipulationAdditions.h */,
88080C17160A706900CCABF2 /* NSArray+MTLManipulationAdditions.m */,
+ 54803A30178829A700011B39 /* NSError+MTLModelException.h */,
+ 54803A31178829A700011B39 /* NSError+MTLModelException.m */,
D0C27D0816110973002FE587 /* NSDictionary+MTLManipulationAdditions.h */,
D0C27D0916110973002FE587 /* NSDictionary+MTLManipulationAdditions.m */,
1ED5B5CE163A4E3C0072668E /* NSObject+MTLComparisonAdditions.h */,
@@ -593,6 +605,7 @@
D0760E7815FFBF330060F550 /* MTLModel.h in Headers */,
D08B5AAE16002694001FE685 /* MTLValueTransformer.h in Headers */,
88080C18160A706900CCABF2 /* NSArray+MTLManipulationAdditions.h in Headers */,
+ 54803A32178829A800011B39 /* NSError+MTLModelException.h in Headers */,
D0C27D0A16110973002FE587 /* NSDictionary+MTLManipulationAdditions.h in Headers */,
D0F117491614C5600092520B /* NSValueTransformer+MTLPredefinedTransformerAdditions.h in Headers */,
1ED5B5D0163A4E3C0072668E /* NSObject+MTLComparisonAdditions.h in Headers */,
@@ -611,6 +624,7 @@
D8A172F91697E87A002CFCC8 /* MTLModel.h in Headers */,
D8A172FA1697E87A002CFCC8 /* MTLValueTransformer.h in Headers */,
D8A172FC1697E87A002CFCC8 /* NSArray+MTLManipulationAdditions.h in Headers */,
+ 54803A33178829A800011B39 /* NSError+MTLModelException.h in Headers */,
D8A172FE1697E87A002CFCC8 /* NSDictionary+MTLManipulationAdditions.h in Headers */,
D8A173021697E87A002CFCC8 /* NSValueTransformer+MTLPredefinedTransformerAdditions.h in Headers */,
D8A173031697E87A002CFCC8 /* NSObject+MTLComparisonAdditions.h in Headers */,
@@ -868,6 +882,7 @@
D0F1174A1614C5600092520B /* NSValueTransformer+MTLPredefinedTransformerAdditions.m in Sources */,
1ED5B5D1163A4E3C0072668E /* NSObject+MTLComparisonAdditions.m in Sources */,
D01BD09F16CB432D00EC95C7 /* MTLJSONAdapter.m in Sources */,
+ 54803A34178829A800011B39 /* NSError+MTLModelException.m in Sources */,
D01BD0B116CB52E800EC95C7 /* MTLModel+NSCoding.m in Sources */,
D058FE2116EFB3D2009DFB47 /* MTLReflection.m in Sources */,
D0E51939170A06CF00E8897B /* MTLManagedObjectAdapter.m in Sources */,
@@ -883,6 +898,7 @@
files = (
D0760EC415FFCA250060F550 /* MTLModelSpec.m in Sources */,
D0760EC915FFCA4E0060F550 /* MTLTestModel.m in Sources */,
+ 54803A3B17882CCD00011B39 /* MTLErrorModelExceptionSpec.m in Sources */,
D08B5AB216002A23001FE685 /* MTLValueTransformerSpec.m in Sources */,
88080C1D160A719D00CCABF2 /* MTLArrayManipulationSpec.m in Sources */,
D064BA341613BA75004CA27A /* MTLTestNotificationObserver.m in Sources */,
@@ -909,6 +925,7 @@
D0F1174B1614C5600092520B /* NSValueTransformer+MTLPredefinedTransformerAdditions.m in Sources */,
1ED5B5D2163A4E3C0072668E /* NSObject+MTLComparisonAdditions.m in Sources */,
D01BD0A016CB432D00EC95C7 /* MTLJSONAdapter.m in Sources */,
+ 54803A35178829A800011B39 /* NSError+MTLModelException.m in Sources */,
D01BD0B216CB52E800EC95C7 /* MTLModel+NSCoding.m in Sources */,
D058FE2216EFB3D2009DFB47 /* MTLReflection.m in Sources */,
D0E5193A170A06CF00E8897B /* MTLManagedObjectAdapter.m in Sources */,
@@ -924,6 +941,7 @@
files = (
D0760EC515FFCA250060F550 /* MTLModelSpec.m in Sources */,
D0760ECA15FFCA4E0060F550 /* MTLTestModel.m in Sources */,
+ 54803A3C17882CCD00011B39 /* MTLErrorModelExceptionSpec.m in Sources */,
D08B5AB316002A23001FE685 /* MTLValueTransformerSpec.m in Sources */,
88080C1E160A719D00CCABF2 /* MTLArrayManipulationSpec.m in Sources */,
D064BA351613BA75004CA27A /* MTLTestNotificationObserver.m in Sources */,
View
@@ -6,6 +6,7 @@
// Copyright (c) 2012 GitHub. All rights reserved.
//
+#import "NSError+MTLModelException.h"
#import "MTLModel.h"
#import "EXTRuntimeExtensions.h"
#import "EXTScope.h"
@@ -17,15 +18,6 @@
#import "MTLJSONAdapter.h"
#import "MTLModel+NSCoding.h"
-// The domain for errors originating from MTLModel.
-static NSString * const MTLModelErrorDomain = @"MTLModelErrorDomain";
-
-// An exception was thrown and caught.
-static const NSInteger MTLModelErrorExceptionThrown = 1;
-
-// Associated with the NSException that was caught.
-static NSString * const MTLModelThrownExceptionErrorKey = @"MTLModelThrownException";
-
// Used to cache the reflection performed in +propertyKeys.
static void *MTLModelCachedPropertyKeysKey = &MTLModelCachedPropertyKeysKey;
@@ -77,13 +69,7 @@ - (instancetype)initWithDictionary:(NSDictionary *)dictionary error:(NSError **)
@throw ex;
#else
if (error != NULL) {
- NSDictionary *userInfo = @{
- NSLocalizedDescriptionKey: ex.description,
- NSLocalizedFailureReasonErrorKey: ex.reason,
- MTLModelThrownExceptionErrorKey: ex
- };
-
- *error = [NSError errorWithDomain:MTLModelErrorDomain code:MTLModelErrorExceptionThrown userInfo:userInfo];
+ *error = [NSError mtl_modelErrorWithException:ex];
}
return nil;
@@ -0,0 +1,23 @@
+//
+// NSError+MTLModelException.h
+// Mantle
+//
+// Created by Robert Böhnke on 7/6/13.
+// Copyright (c) 2013 GitHub. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSError (MTLModelException)
+
+// Creates a new error for an exception that occured during updating an
+// MTLModel.
+//
+// exception - The exception that was thrown while updating the model.
+// This argument must not be nil.
+//
+// Returns an error that takes its localized description and failure reason
+// from the exception.
++ (instancetype)mtl_modelErrorWithException:(NSException *)exception;
+
+@end
@@ -0,0 +1,37 @@
+//
+// NSError+MTLModelException.m
+// Mantle
+//
+// Created by Robert Böhnke on 7/6/13.
+// Copyright (c) 2013 GitHub. All rights reserved.
+//
+
+#import "MTLModel.h"
+
+#import "NSError+MTLModelException.h"
+
+// The domain for errors originating from MTLModel.
+static NSString * const MTLModelErrorDomain = @"MTLModelErrorDomain";
+
+// An exception was thrown and caught.
+static const NSInteger MTLModelErrorExceptionThrown = 1;
+
+// Associated with the NSException that was caught.
+static NSString * const MTLModelThrownExceptionErrorKey = @"MTLModelThrownException";
+
+@implementation NSError (MTLModelException)
+
++ (instancetype)mtl_modelErrorWithException:(NSException *)exception
+{
+ NSParameterAssert(exception != nil);
+
+ NSDictionary *userInfo = @{
+ NSLocalizedDescriptionKey: exception.description,
+ NSLocalizedFailureReasonErrorKey: exception.reason,
+ MTLModelThrownExceptionErrorKey: exception
+ };
+
+ return [NSError errorWithDomain:MTLModelErrorDomain code:MTLModelErrorExceptionThrown userInfo:userInfo];
+}
+
+@end
@@ -0,0 +1,27 @@
+//
+// MTLErrorModelExceptionSpec.m
+// Mantle
+//
+// Created by Robert Böhnke on 7/6/13.
+// Copyright (c) 2013 GitHub. All rights reserved.
+//
+
+#import "NSError+MTLModelException.h"
+
+SpecBegin(MTLErrorModelException)
+
+describe(@"+mtl_modelErrorWithException:", ^{
+ it(@"should return a new error for that exception", ^{
+ NSException *exception = [NSException exceptionWithName:@"MTLTestException" reason:@"Just Testing" userInfo:nil];
+
+ NSError *error = [NSError mtl_modelErrorWithException:exception];
+
+ expect(error).toNot.beNil();
+ expect(error.localizedDescription).to.equal(@"Just Testing");
+ expect(error.localizedFailureReason).to.equal(@"Just Testing");
+
+ expect(error.userInfo[@"MTLModelThrownException"]).to.equal(exception);
+ });
+});
+
+SpecEnd

0 comments on commit ffb0851

Please sign in to comment.