Skip to content

Commit 61d651d

Browse files
[macOS] Use FLEMethodError on color panel plugin bad arguments (#252)
1 parent 0adf81e commit 61d651d

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

plugins/color_panel/macos/FLEColorPanelPlugin.mm

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ @implementation FLEColorPanelPlugin {
2424
}
2525

2626
+ (void)registerWithRegistrar:(id<FLEPluginRegistrar>)registrar {
27-
FLEMethodChannel* channel = [FLEMethodChannel
28-
methodChannelWithName:@(plugins_color_panel::kChannelName)
29-
binaryMessenger:registrar.messenger
30-
codec:[FLEJSONMethodCodec sharedInstance]];
31-
FLEColorPanelPlugin* instance = [[FLEColorPanelPlugin alloc] initWithChannel:channel];
27+
FLEMethodChannel *channel =
28+
[FLEMethodChannel methodChannelWithName:@(plugins_color_panel::kChannelName)
29+
binaryMessenger:registrar.messenger
30+
codec:[FLEJSONMethodCodec sharedInstance]];
31+
FLEColorPanelPlugin *instance = [[FLEColorPanelPlugin alloc] initWithChannel:channel];
3232
[registrar addMethodCallDelegate:instance channel:channel];
3333
}
3434

35-
- (instancetype)initWithChannel:(FLEMethodChannel*)channel {
35+
- (instancetype)initWithChannel:(FLEMethodChannel *)channel {
3636
self = [super init];
3737
if (self) {
3838
_channel = channel;
@@ -45,26 +45,29 @@ - (instancetype)initWithChannel:(FLEMethodChannel*)channel {
4545
* panel channel.
4646
*/
4747
- (void)handleMethodCall:(FLEMethodCall *)call result:(FLEMethodResult)result {
48-
BOOL handled = YES;
48+
id methodResult = nil;
4949
if ([call.methodName isEqualToString:@(plugins_color_panel::kShowColorPanelMethod)]) {
5050
if ([call.arguments isKindOfClass:[NSDictionary class]]) {
5151
BOOL showAlpha =
5252
[[call.arguments valueForKey:@(plugins_color_panel::kColorPanelShowAlpha)] boolValue];
5353
[self showColorPanelWithAlpha:showAlpha];
5454
} else {
55-
NSLog(@"Malformed call for %@. Expected an NSDictionary but got %@",
56-
@(plugins_color_panel::kShowColorPanelMethod),
57-
NSStringFromClass([call.arguments class]));
58-
handled = NO;
55+
NSString *errorString =
56+
[NSString stringWithFormat:@"Malformed call for %@. Expected an NSDictionary but got %@",
57+
@(plugins_color_panel::kShowColorPanelMethod),
58+
NSStringFromClass([call.arguments class])];
59+
methodResult = [[FLEMethodError alloc] initWithCode:@"Bad arguments"
60+
message:errorString
61+
details:nil];
5962
}
6063
} else if ([call.methodName isEqualToString:@(plugins_color_panel::kHideColorPanelMethod)]) {
6164
[self hideColorPanel];
6265
} else {
63-
handled = NO;
66+
methodResult = FLEMethodNotImplemented;
6467
}
65-
// Send an immediate empty success message for handled messages, since the actual color data
66-
// will be provided in follow-up messages.
67-
result(handled ? nil : FLEMethodNotImplemented);
68+
// If no errors are generated, send an immediate empty success message for handled messages, since
69+
// the actual color data will be provided in follow-up messages.
70+
result(methodResult);
6871
}
6972

7073
/**
@@ -114,7 +117,7 @@ - (void)selectedColorDidChange {
114117
NSColor *color = [NSColorPanel sharedColorPanel].color;
115118
NSDictionary *colorDictionary = [self dictionaryWithColor:color];
116119
[_channel invokeMethod:@(plugins_color_panel::kColorSelectedCallbackMethod)
117-
arguments:colorDictionary];
120+
arguments:colorDictionary];
118121
}
119122

120123
/**
@@ -138,8 +141,7 @@ - (NSDictionary *)dictionaryWithColor:(NSColor *)color {
138141

139142
- (void)windowWillClose:(NSNotification *)notification {
140143
[self removeColorPanelConnections];
141-
[_channel invokeMethod:@(plugins_color_panel::kClosedCallbackMethod)
142-
arguments:nil];
144+
[_channel invokeMethod:@(plugins_color_panel::kClosedCallbackMethod) arguments:nil];
143145
}
144146

145147
@end

0 commit comments

Comments
 (0)