Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TaskError should report which task failed #53

Merged
merged 2 commits into from
Feb 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
}
}