Skip to content

Commit

Permalink
Merge pull request #8 from Code-Hex/fix/error-handling
Browse files Browse the repository at this point in the history
improved error handling
  • Loading branch information
Code-Hex authored Sep 15, 2021
2 parents 7475877 + 8c410e9 commit eccba26
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
31 changes: 23 additions & 8 deletions objcutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ NSInteger getNSErrorCode(void *err)
return (NSInteger)[(NSError *)err code];
}
typedef struct NSErrorFlat {
const char *domain;
const char *localizedDescription;
const char *userinfo;
int code;
} NSErrorFlat;
NSErrorFlat convertNSError2Flat(void *err)
{
NSErrorFlat ret;
ret.domain = getNSErrorDomain(err);
ret.localizedDescription = getNSErrorLocalizedDescription(err);
ret.userinfo = getNSErrorUserInfo(err);
ret.code = (int)getNSErrorCode(err);
return ret;
}
void *makeNSMutableArray(unsigned long cap)
{
return [[NSMutableArray alloc] initWithCapacity:(NSUInteger)cap];
Expand Down Expand Up @@ -175,19 +193,16 @@ func (n *NSError) Error() string {
)
}

// TODO(codehex): improvement (3 times called C functions now)
func newNSError(p unsafe.Pointer) *NSError {
if !hasNSError(p) {
return nil
}
domain := (*char)(C.getNSErrorDomain(p))
description := (*char)(C.getNSErrorLocalizedDescription(p))
userInfo := (*char)(C.getNSErrorUserInfo(p))
nsError := C.convertNSError2Flat(p)
return &NSError{
Domain: domain.String(),
Code: int(C.getNSErrorCode(p)),
LocalizedDescription: description.String(),
UserInfo: userInfo.String(), // NOTE(codehex): maybe we can convert to map[string]interface{}
Domain: (*char)(nsError.domain).String(),
Code: int((nsError.code)),
LocalizedDescription: (*char)(nsError.localizedDescription).String(),
UserInfo: (*char)(nsError.userinfo).String(), // NOTE(codehex): maybe we can convert to map[string]interface{}
}
}

Expand Down
16 changes: 6 additions & 10 deletions virtualization.m
Original file line number Diff line number Diff line change
Expand Up @@ -357,16 +357,12 @@ void setStorageDevicesVZVirtualMachineConfiguration(void *config,
*/
void *newVZDiskImageStorageDeviceAttachment(const char *diskPath, bool readOnly, void **error)
{
VZDiskImageStorageDeviceAttachment *ret;
@autoreleasepool {
NSString *diskPathNSString = [NSString stringWithUTF8String:diskPath];
NSURL *diskURL = [NSURL fileURLWithPath:diskPathNSString];
ret = [[VZDiskImageStorageDeviceAttachment alloc]
initWithURL:diskURL
readOnly:(BOOL)readOnly
error:(NSError * _Nullable * _Nullable)error];
}
return ret;
NSString *diskPathNSString = [NSString stringWithUTF8String:diskPath];
NSURL *diskURL = [NSURL fileURLWithPath:diskPathNSString];
return [[VZDiskImageStorageDeviceAttachment alloc]
initWithURL:diskURL
readOnly:(BOOL)readOnly
error:(NSError * _Nullable * _Nullable)error];
}


Expand Down

0 comments on commit eccba26

Please sign in to comment.