chore: Make some tryGet errors verbose and display line
tryGet messages can spam the logs and may be better if they are just verbose instead of warnings. Also now we display the line where this tryGet call was.
This commit is contained in:
parent
2383cef438
commit
6a3c25a8bd
|
|
@ -39,7 +39,7 @@ class _RequiredLog implements TryGet {
|
|||
const _RequiredLog();
|
||||
@override
|
||||
void call(String key, Type expected, Type actual) => Logs().w(
|
||||
'Expected "$expected" in event content for the Key "$key" but got "$actual".');
|
||||
'Expected required "$expected" in event content for the Key "$key" but got "$actual" at ${StackTrace.current.firstLine}');
|
||||
}
|
||||
|
||||
class _OptionalLog implements TryGet {
|
||||
|
|
@ -48,7 +48,7 @@ class _OptionalLog implements TryGet {
|
|||
void call(String key, Type expected, Type actual) {
|
||||
if (actual != Null) {
|
||||
Logs().w(
|
||||
'Expected "$expected" in event content for the Key "$key" but got "$actual".');
|
||||
'Expected optional "$expected" in event content for the Key "$key" but got "$actual" at ${StackTrace.current.firstLine}');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -79,8 +79,8 @@ extension TryGetMapExtension on Map<String, dynamic> {
|
|||
// copy entries to ensure type check failures here and not an access
|
||||
return value.cast<T>().toList();
|
||||
} catch (_) {
|
||||
Logs()
|
||||
.w('Unable to create "List<$T>" in event content for the key "$key"');
|
||||
Logs().v(
|
||||
'Unable to create "List<$T>" in event content for the key "$key" at ${StackTrace.current.firstLine}');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -95,9 +95,20 @@ extension TryGetMapExtension on Map<String, dynamic> {
|
|||
// copy map to ensure type check failures here and not an access
|
||||
return Map.from(value.cast<A, B>());
|
||||
} catch (_) {
|
||||
Logs().w(
|
||||
'Unable to create "Map<$A,$B>" in event content for the key "$key"');
|
||||
Logs().v(
|
||||
'Unable to create "Map<$A,$B>" in event content for the key "$key" at ${StackTrace.current.firstLine}');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension on StackTrace {
|
||||
String get firstLine {
|
||||
final lines = toString().split('\n');
|
||||
return lines.length >= 3
|
||||
? lines[2].replaceFirst('#2 ', '')
|
||||
: lines.isNotEmpty
|
||||
? lines.first
|
||||
: '(unknown position)';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ import 'package:test/test.dart';
|
|||
|
||||
void main() {
|
||||
group('Try-get-map-extension', () {
|
||||
Logs().level = Level.error;
|
||||
Logs().level = Level.verbose;
|
||||
test('it should work', () {
|
||||
final data = <String, dynamic>{
|
||||
'str': 'foxies',
|
||||
|
|
@ -40,12 +40,12 @@ void main() {
|
|||
expect(data.tryGet<int>('str'), null);
|
||||
expect(data.tryGet<int>('int'), 42);
|
||||
expect(data.tryGet<List>('list'), [2, 3, 4]);
|
||||
expect(data.tryGet<Map<String, dynamic>>('map')?.tryGet<String>('beep'),
|
||||
expect(data.tryGetMap<String, dynamic>('map')?.tryGet<String>('beep'),
|
||||
'boop');
|
||||
expect(data.tryGet<Map<String, dynamic>>('map')?.tryGet<String>('meep'),
|
||||
null);
|
||||
expect(data.tryGet<Map<String, dynamic>>('pam')?.tryGet<String>('beep'),
|
||||
null);
|
||||
expect(
|
||||
data.tryGetMap<String, dynamic>('map')?.tryGet<String>('meep'), null);
|
||||
expect(
|
||||
data.tryGetMap<String, dynamic>('pam')?.tryGet<String>('beep'), null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue