From 08d238434de784c5de059efec7a4d59412966d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B8=CC=86=20=D0=93=D0=B0?= =?UTF-8?q?=D0=BB=D0=B5=D0=B7=D0=B4=D0=B8=D0=BD=D0=BE=D0=B2?= Date: Tue, 12 Apr 2016 03:12:03 +0300 Subject: [PATCH] use ErrorType + @noescape --- Try/WBTry.h | 2 +- Try/WBTry.m | 2 +- Try/trap.swift | 18 +++++++++--------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Try/WBTry.h b/Try/WBTry.h index 05e40d9..695331a 100644 --- a/Try/WBTry.h +++ b/Try/WBTry.h @@ -22,6 +22,6 @@ @param finallyBlock An optional block that is called in the \@finally block. */ -+ (void)tryBlock:(nonnull void (^)(void))tryBlock catchAndRethrowBlock:(nullable BOOL (^)(_Nonnull id))catchAndRethrowBlock finallyBlock:(nullable void (^)(void))finallyBlock; ++ (void)tryBlock:( __attribute__((noescape)) void (^ _Nonnull )(void)) tryBlock catchAndRethrowBlock:(nullable BOOL (^)(_Nonnull id))catchAndRethrowBlock finallyBlock:(nullable void (^)(void))finallyBlock; @end diff --git a/Try/WBTry.m b/Try/WBTry.m index 8238f0c..aa69d68 100644 --- a/Try/WBTry.m +++ b/Try/WBTry.m @@ -9,7 +9,7 @@ @implementation WBTry -+ (void)tryBlock:(nonnull void (^)(void))tryBlock catchAndRethrowBlock:(nullable BOOL (^)(_Nonnull id))catchAndRethrowBlock finallyBlock:(nullable void (^)(void))finallyBlock { ++ (void)tryBlock:( __attribute__((noescape)) void (^ _Nonnull )(void)) tryBlock catchAndRethrowBlock:(nullable BOOL (^)(_Nonnull id))catchAndRethrowBlock finallyBlock:(nullable void (^)(void))finallyBlock { @try { tryBlock(); } diff --git a/Try/trap.swift b/Try/trap.swift index 7710eb6..0083813 100644 --- a/Try/trap.swift +++ b/Try/trap.swift @@ -7,25 +7,25 @@ import Foundation -public let tryErrorDomain = "Try" -public let tryExceptionErrorCode = 1 -public let tryExceptionErrorKey = "exception" +enum TryError: ErrorType { + case Exception(e: NSException) +} /** Wraps a closure in a `WBTry.tryBlock` to catch Objective-C exceptions using the Swift error handling model. - parameter block: The block of code to run within a `WBTry.tryBlock`. - - throws: Throws an `NSError` if the wrapped code throws an exception. + - throws: Throws a `TryError` if the wrapped code throws an exception. */ -public func trap(block: () -> Void) throws { - var exception: AnyObject? +public func trap(@noescape block: () -> Void) throws { + var exception: NSException? WBTry.tryBlock(block, catchAndRethrowBlock: { - exception = $0 + exception = $0 as? NSException return false }, finallyBlock: nil) if let e = exception { - throw NSError(domain: tryErrorDomain, code: tryExceptionErrorCode, userInfo: [tryExceptionErrorKey: e]) + throw TryError.Exception(e: e) } -} +} \ No newline at end of file