Skip to content

Commit

Permalink
Propagate error message from libviv
Browse files Browse the repository at this point in the history
  • Loading branch information
p00ya committed Sep 20, 2020
1 parent 5b1443c commit eb054c9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion viv/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Manager::NotifyValue(uint8_t const *value, size_t length) {
if (command.ReadPacket(packet) < 0) {
delegate_->DidError(
kVLManagerErrorBadPayload,
command.name() + ": invalid value notification");
command.name() + ": error in response");
return;
}

Expand Down
30 changes: 27 additions & 3 deletions viv/manager_objc_bridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@

const NSErrorDomain VLOManagerErrorDomain = @"VLOManagerErrorDomain";

static NSString *const VLOUserInfoMessageKey = @"message";

/// Wrapper for errors from libviv, with custom description.
@interface VLOError : NSError
- (instancetype)initWithCode:(VLManagerErrorCode)code
message:(NSString *)message;

@end

@implementation VLOError

- (instancetype)initWithCode:(VLManagerErrorCode)code
message:(NSString *)message {
self = [super initWithDomain:VLOManagerErrorDomain
code:code
userInfo:@{VLOUserInfoMessageKey : message}];
return self;
}

- (NSString *)localizedDescription {
return self.userInfo[VLOUserInfoMessageKey];
}

@end

namespace {

/// Implementation of the C++ delegate interface that forwards to the
Expand Down Expand Up @@ -56,9 +81,8 @@ int WriteValue(uint8_t const *value, size_t length) override {
void
DidError(VLManagerErrorCode code, std::string const &&msg) const override {
if ([delegate_ respondsToSelector:@selector(didError:)]) {
NSError *error = [NSError errorWithDomain:VLOManagerErrorDomain
code:code
userInfo:@{}];
NSString *message = [NSString stringWithUTF8String:msg.c_str()];
VLOError *error = [[VLOError alloc] initWithCode:code message:message];
[delegate_ didError:error];
}
}
Expand Down

0 comments on commit eb054c9

Please sign in to comment.