Skip to content

Commit

Permalink
Merge pull request #53 from younata/master
Browse files Browse the repository at this point in the history
TaskError should report which task failed
  • Loading branch information
mdiep committed Feb 24, 2016
2 parents 27deb38 + 9d0b2be commit 5fbadcd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 3 additions & 3 deletions ReactiveTask/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ReactiveCocoa
/// An error originating from ReactiveTask.
public enum TaskError: ErrorType {
/// A shell task exited unsuccessfully.
case ShellTaskFailed(exitCode: Int32, standardError: String?)
case ShellTaskFailed(Task, exitCode: Int32, standardError: String?)

/// An error was returned from a POSIX API.
case POSIXError(Int32)
Expand All @@ -21,8 +21,8 @@ public enum TaskError: ErrorType {
extension TaskError: CustomStringConvertible {
public var description: String {
switch self {
case let .ShellTaskFailed(exitCode, standardError):
var description = "A shell task failed with exit code \(exitCode)"
case let .ShellTaskFailed(task, exitCode, standardError):
var description = "A shell task (\(task)) failed with exit code \(exitCode)"
if let standardError = standardError {
description += ":\n\(standardError)"
}
Expand Down
6 changes: 3 additions & 3 deletions ReactiveTask/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,8 @@ public func launchTask(task: Task, standardInput: SignalProducer<NSData, NoError
rawTask.standardError = stderrPipe.writeHandle

dispatch_group_enter(group)
rawTask.terminationHandler = { task in
let terminationStatus = task.terminationStatus
rawTask.terminationHandler = { nstask in
let terminationStatus = nstask.terminationStatus
if terminationStatus == EXIT_SUCCESS {
// Wait for stderr to finish, then pass
// through stdout.
Expand All @@ -477,7 +477,7 @@ public func launchTask(task: Task, standardInput: SignalProducer<NSData, NoError
.then(stderrAggregated)
.flatMap(.Concat) { data -> SignalProducer<TaskEvent<NSData>, TaskError> in
let errorString = (data.length > 0 ? NSString(data: data, encoding: NSUTF8StringEncoding) as? String : nil)
return SignalProducer(error: .ShellTaskFailed(exitCode: terminationStatus, standardError: errorString))
return SignalProducer(error: .ShellTaskFailed(task, exitCode: terminationStatus, standardError: errorString))
}
.start(observer)
}
Expand Down
12 changes: 12 additions & 0 deletions ReactiveTaskTests/TaskSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,17 @@ class TaskSpec: QuickSpec {
expect(NSString(data: data, encoding: NSUTF8StringEncoding)).to(equal("bar\nbuzz\nfoo\nfuzz\n"))
}
}

it("should error correctly") {
let task = Task("/usr/bin/stat", arguments: [ "not-a-real-file" ])
let result = launchTask(task)
.wait()

expect(result).notTo(beNil())
expect(result.error).toNot(beNil())
if let error = result.error {
expect(error.description) == "A shell task (/usr/bin/stat not-a-real-file) failed with exit code 1:\nstat: not-a-real-file: stat: No such file or directory\n"
}
}
}
}

0 comments on commit 5fbadcd

Please sign in to comment.