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

improved error handling #8

Merged
merged 2 commits into from
Sep 15, 2021
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
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];
Comment on lines -362 to -363
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These codes do not use alloc or init, new to create new objects, so there was no need to release them.

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