Code Monkey home page Code Monkey logo

nsxreturnthrowerror's Introduction

NSXReturnThrowError does two things:

  1. Eases wrapping various kinds of error codes (Cocoa, Posix, Mach, Carbon) into NSError objects.

  2. Enhances NSError by adding origin information to the error instance.
    Origin information includes the actual line of code that returned the error, as well as the file+line+function/method name.

A big NSXReturnThrowError feature is that it deduces the correct NSError error domain based on the wrapped code's return type+value. Bonus: it does so without requiring Objective-C++, relying on @encode acrobatics instead.

NSXReturnThrowError is compatible with Mac OS X 10.3-10.7 and every version of iOS.

###Usage

NSXReturnThrowError handles both types of error handling: explicit returning of NSError objects and raising NSExceptions.

Use NSXReturnError() if you're returning NSError objects explicitly:

- (id)demoReturnError:(NSError**)error_ {
	id result = nil;
	NSError *error = nil;
	
	NSXReturnError(SomeCarbonFunction());
	if (!error)
		NSXReturnError(someposixfunction());
	if (!error)
		NSXReturnError(some_mach_function());
	if (!error)
		NSXReturnError([SomeCocoaClass newObject]);
	
	if (error_) *error_ = error;
	return result;
}

Use NSXThrowError() if you'd prefer to raise NSException objects:

- (id)demo {
	id result = nil;
	
	NSXThrowError(SomeCarbonFunction());
	NSXThrowError(someposixfunction());
	NSXThrowError(some_mach_function());
	NSXThrowError([SomeCocoaClass newObject]);
	
	return result;
}

The current structure of the raised NSException object is that it's a normal NSException whose name is "NSError". The actual error object is hung off the exception's userInfo dictionary with the key of @"error". You can use code like this to report it:

if (error)
	NSLog(@"error:%@ userInfo:%@", error, [error userInfo]);

Or use JRLog's JRLogNSError(error), which accomplishes the same thing.

nsxreturnthrowerror's People

Contributors

rentzsch avatar

Watchers

Zev Eisenberg avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.