Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moving NSError+LessTerrible

  • Loading branch information...
commit 00d0f06e0e75a667624b0d281f11ec00e2ccf87c 1 parent ac407c3
@drewcrawford authored
View
20 DCAKit/NSError+LessTerrible.h
@@ -0,0 +1,20 @@
+//
+// NSError+LessTerrible.h
+// Adrenaline
+//
+// Created by Drew Crawford on 1/9/13.
+// Copyright (c) 2013 DrewCrawfordApps. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSError (LessSuck)
+/**Less terrible errors */
+-(NSString*) lessTerribleFailureReason;
+-(void) present;
+
+//any keys you set in here will get reported with errors...
+@property (readonly) NSMutableDictionary *environment;
+
++(void) setExceptionalAPIKey:(NSString*) exceptionalKey;
+@end
View
103 DCAKit/NSError+LessTerrible.m
@@ -0,0 +1,103 @@
+//
+// NSError+LessTerrible.m
+// Adrenaline
+//
+// Created by Drew Crawford on 1/9/13.
+// Copyright (c) 2013 DrewCrawfordApps. All rights reserved.
+//
+#define ISO_TIMEZONE_UTC_FORMAT @"Z"
+#define ISO_TIMEZONE_OFFSET_FORMAT @"%+02d%02d"
+
+#import "NSError+LessTerrible.h"
+#import <UIKit/UIKit.h>
+
+static NSString *exceptionalAPIKey;
+static NSMutableDictionary *environment;
+
+@implementation NSError (LessSuck)
+- (NSString *)lessTerribleFailureReason {
+ if ([self domain]==NSURLErrorDomain && [self code]==NSURLErrorUserCancelledAuthentication) {
+ return @"User unauthorized.";
+ }
+ return [self localizedDescription];
+}
+
+- (NSString*) lessTerribleRecoverySuggestion {
+ if ([self domain]==NSURLErrorDomain && [self code]==NSURLErrorUserCancelledAuthentication) {
+ return @"Check your login credentials and try again.";
+ }
+ return [self localizedRecoverySuggestion];
+}
+
+-(void) present {
+ [self log];
+ UIAlertView *uav = [[UIAlertView alloc] initWithTitle:[self lessTerribleFailureReason] message:[self lessTerribleRecoverySuggestion] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [uav show];
+ });
+
+}
+
+#pragma mark - Exceptional
+
+
+-(NSString *) strFromISO8601:(NSDate *) date {
+ static NSDateFormatter* sISO8601 = nil;
+
+ if (!sISO8601) {
+ sISO8601 = [[NSDateFormatter alloc] init];
+
+ NSTimeZone *timeZone = [NSTimeZone localTimeZone];
+ int offset = [timeZone secondsFromGMT];
+
+ NSMutableString *strFormat = [NSMutableString stringWithString:@"yyyyMMdd'T'HH:mm:ss"];
+ offset /= 60; //bring down to minutes
+ if (offset == 0)
+ [strFormat appendString:ISO_TIMEZONE_UTC_FORMAT];
+ else
+ [strFormat appendFormat:ISO_TIMEZONE_OFFSET_FORMAT, offset / 60, offset % 60];
+
+ [sISO8601 setTimeStyle:NSDateFormatterFullStyle];
+ [sISO8601 setDateFormat:strFormat];
+ }
+ return[sISO8601 stringFromDate:date];
+}
+
++(void) setExceptionalAPIKey:(NSString*) exceptionalKey {
+ exceptionalAPIKey = exceptionalKey;
+ environment = [[NSMutableDictionary alloc] init];
+}
+
+-(NSMutableDictionary*) environment {
+ return environment;
+}
+
+//http://docs.exceptional.io/api/publish/
+-(void) log {
+ if (!exceptionalAPIKey) {
+ NSLog(@"Cannot log an error because no exceptional API key is set. Consider creating one.");
+ return;
+ }
+ NSDictionary *appDict = [[NSBundle mainBundle] infoDictionary];
+ NSDictionary *client = @{@"name":appDict[@"CFBundleName"],@"version":appDict[@"CFBundleVersion"],@"build":appDict[@"CFBundleShortVersionString"],@"deviceName":UIDevice.currentDevice.name,@"OSVersion":UIDevice.currentDevice.systemVersion,@"model":UIDevice.currentDevice.model,@"orientation":@(UIDevice.currentDevice.orientation),@"userInfo":[self.userInfo description]};
+ for (id key in client.allKeys) {
+ environment[[NSString stringWithFormat:@"__client_%@",key]]=client[key];
+ }
+
+
+
+ NSDictionary *dict = @{@"exception":@{@"backtrace" : [NSThread callStackSymbols],@"exception_class":self.domain,@"message":self.lessTerribleFailureReason,@"occurred_at":[self strFromISO8601:[NSDate date]]},@"application_environment":@{@"application_root_directory":@"Steve Jobs says no",@"env":environment}};
+ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.exceptional.io/api/errors?api_key=%@&protocol_version=6",exceptionalAPIKey]]];
+ NSAssert([NSJSONSerialization isValidJSONObject:dict], @"Not a valid json object?");
+
+ NSError *jsonErr = nil;
+ NSData *errData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&jsonErr];
+ [request setHTTPBody:errData];
+ [request setHTTPMethod:@"POST"];
+ NSURLResponse *response = nil;
+ NSError *err = nil;
+ [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
+
+
+}
+@end
View
12 pre-commit.py
@@ -10,7 +10,9 @@
PROJECT_NAME = "DCAKit"
WORKSPACE_NAME = "DCAKit.xcodeproj/project.xcworkspace"
TEST_SCHEME = "DCAKit"
+TEST_CUSTOMFLAGS = ""
BUILD_SCHEME = "DCAKit"
+BUILD_CUSTOMFLAGS = ""
TEST_SUITE_NAME = "DCAKitTests.octest"
#no config below - paste over me
@@ -19,13 +21,12 @@
# v0.1 - submodule support
# v0.2 - improved test output regex
# v0.3 - better git_index_file workaround
-
-import os
-print os.getcwd()
+# v0.4 - support custom flags
def getSO(cmd):
import subprocess
+ print "run",cmd
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output,stderr = proc.communicate()
proc.wait() # this might hang with a LOT of output
@@ -34,6 +35,7 @@ def getSO(cmd):
def getO(cmd):
import subprocess
+ print "run",cmd
return subprocess.check_output(cmd, shell=True)
@@ -87,7 +89,7 @@ def success():
print "build/analyze/warning check"
- (code,output) = getSO("xcodebuild -configuration Release -sdk iphonesimulator -workspace {WORKSPACE_NAME} -IDEBuildOperationMaxNumberOfConcurrentCompile=1 -scheme {SCHEME_NAME} RUN_CLANG_STATIC_ANALYZER=YES clean build ".format(SCHEME_NAME=BUILD_SCHEME,WORKSPACE_NAME=WORKSPACE_NAME))
+ (code,output) = getSO("xcodebuild -configuration Release -sdk iphonesimulator -workspace {WORKSPACE_NAME} -IDEBuildOperationMaxNumberOfConcurrentCompile=1 -scheme {SCHEME_NAME} RUN_CLANG_STATIC_ANALYZER=YES {CUSTOM_FLAGS} clean build ".format(SCHEME_NAME=BUILD_SCHEME,WORKSPACE_NAME=WORKSPACE_NAME,CUSTOM_FLAGS=BUILD_CUSTOMFLAGS))
@@ -110,7 +112,7 @@ def success():
print "unit test check"
#run the unit tests
- (code,output) = getSO("xcodebuild -sdk iphonesimulator -configuration UnitTest -workspace {WORKSPACE_NAME} -scheme {SCHEME_NAME} RUN_UNIT_TEST_WITH_IOS_SIM=YES clean build".format(SCHEME_NAME=TEST_SCHEME,WORKSPACE_NAME=WORKSPACE_NAME))
+ (code,output) = getSO("xcodebuild -sdk iphonesimulator -configuration UnitTest -workspace {WORKSPACE_NAME} -scheme {SCHEME_NAME} RUN_UNIT_TEST_WITH_IOS_SIM=YES {CUSTOM_FLAGS} clean build".format(SCHEME_NAME=TEST_SCHEME,WORKSPACE_NAME=WORKSPACE_NAME,CUSTOM_FLAGS=TEST_CUSTOMFLAGS))
if code != 0:
print output
Please sign in to comment.
Something went wrong with that request. Please try again.