Skip to content

Commit

Permalink
Convert libGit2Error free function to an NSError convenience initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrubin committed Dec 18, 2016
1 parent 3c3c3ff commit 94c7de8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
38 changes: 20 additions & 18 deletions SwiftGit2/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@ import libgit2

public let libGit2ErrorDomain = "org.libgit2.libgit2"

/// Returns an NSError with an error domain and message for libgit2 errors.
///
/// :param: errorCode An error code returned by a libgit2 function.
/// :param: libGit2PointOfFailure The name of the libgit2 function that produced the
/// error code.
/// :returns: An NSError with a libgit2 error domain, code, and message.
internal func libGit2Error(_ errorCode: Int32, libGit2PointOfFailure: String? = nil) -> NSError {
let code = Int(errorCode)
var userInfo: [String: String] = [:]
internal extension NSError {
/// Returns an NSError with an error domain and message for libgit2 errors.
///
/// :param: errorCode An error code returned by a libgit2 function.
/// :param: libGit2PointOfFailure The name of the libgit2 function that produced the
/// error code.
/// :returns: An NSError with a libgit2 error domain, code, and message.
internal convenience init(gitError errorCode: Int32, pointOfFailure: String? = nil) {
let code = Int(errorCode)
var userInfo: [String: String] = [:]

if let message = errorMessage(errorCode) {
userInfo[NSLocalizedDescriptionKey] = message
} else {
userInfo[NSLocalizedDescriptionKey] = "Unknown libgit2 error."
}
if let message = errorMessage(errorCode) {
userInfo[NSLocalizedDescriptionKey] = message
} else {
userInfo[NSLocalizedDescriptionKey] = "Unknown libgit2 error."
}

if let pointOfFailure = libGit2PointOfFailure {
userInfo[NSLocalizedFailureReasonErrorKey] = "\(pointOfFailure) failed."
}
if let pointOfFailure = pointOfFailure {
userInfo[NSLocalizedFailureReasonErrorKey] = "\(pointOfFailure) failed."
}

return NSError(domain: libGit2ErrorDomain, code: code, userInfo: userInfo)
self.init(domain: libGit2ErrorDomain, code: code, userInfo: userInfo)
}
}

/// Returns the libgit2 error message for the given error code.
Expand Down
22 changes: 11 additions & 11 deletions SwiftGit2/Repository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ final public class Repository {
}

if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_repository_open"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_repository_open"))
}

let repository = Repository(pointer!)
Expand Down Expand Up @@ -142,7 +142,7 @@ final public class Repository {
}

if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_clone"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_clone"))
}

let repository = Repository(pointer!)
Expand Down Expand Up @@ -191,7 +191,7 @@ final public class Repository {
let result = git_object_lookup(&pointer, self.pointer, &oid, type)

if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_object_lookup"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_object_lookup"))
}

let value = transform(pointer!)
Expand Down Expand Up @@ -306,7 +306,7 @@ final public class Repository {

if result != GIT_OK.rawValue {
pointer.deallocate(capacity: 1)
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_remote_list"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_remote_list"))
}

let strarray = pointer.pointee
Expand All @@ -333,7 +333,7 @@ final public class Repository {
let result = git_remote_lookup(&pointer, self.pointer, name)

if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_remote_lookup"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_remote_lookup"))
}

let value = Remote(pointer!)
Expand All @@ -350,7 +350,7 @@ final public class Repository {

if result != GIT_OK.rawValue {
pointer.deallocate(capacity: 1)
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_reference_list"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_reference_list"))
}

let strarray = pointer.pointee
Expand Down Expand Up @@ -381,7 +381,7 @@ final public class Repository {
let result = git_reference_lookup(&pointer, self.pointer, name)

if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_reference_lookup"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_reference_lookup"))
}

let value = referenceWithLibGit2Reference(pointer!)
Expand Down Expand Up @@ -437,7 +437,7 @@ final public class Repository {
var pointer: OpaquePointer? = nil
let result = git_repository_head(&pointer, self.pointer)
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_repository_head"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_repository_head"))
}
let value = referenceWithLibGit2Reference(pointer!)
git_reference_free(pointer)
Expand All @@ -452,7 +452,7 @@ final public class Repository {
var oid = oid.oid
let result = git_repository_set_head_detached(self.pointer, &oid)
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_repository_set_head"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_repository_set_head"))
}
return Result.success()
}
Expand All @@ -464,7 +464,7 @@ final public class Repository {
public func setHEAD(_ reference: ReferenceType) -> Result<(), NSError> {
let result = git_repository_set_head(self.pointer, reference.longName)
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_repository_set_head"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_repository_set_head"))
}
return Result.success()
}
Expand All @@ -479,7 +479,7 @@ final public class Repository {

let result = git_checkout_head(self.pointer, &options)
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_checkout_head"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_checkout_head"))
}

return Result.success()
Expand Down

0 comments on commit 94c7de8

Please sign in to comment.