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:
Krille Fear 2021-11-24 07:56:42 +01:00
parent 2383cef438
commit 6a3c25a8bd
2 changed files with 23 additions and 12 deletions

View File

@ -39,7 +39,7 @@ class _RequiredLog implements TryGet {
const _RequiredLog(); const _RequiredLog();
@override @override
void call(String key, Type expected, Type actual) => Logs().w( 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 { class _OptionalLog implements TryGet {
@ -48,7 +48,7 @@ class _OptionalLog implements TryGet {
void call(String key, Type expected, Type actual) { void call(String key, Type expected, Type actual) {
if (actual != Null) { if (actual != Null) {
Logs().w( 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 // copy entries to ensure type check failures here and not an access
return value.cast<T>().toList(); return value.cast<T>().toList();
} catch (_) { } catch (_) {
Logs() Logs().v(
.w('Unable to create "List<$T>" in event content for the key "$key"'); 'Unable to create "List<$T>" in event content for the key "$key" at ${StackTrace.current.firstLine}');
return null; return null;
} }
} }
@ -95,9 +95,20 @@ extension TryGetMapExtension on Map<String, dynamic> {
// copy map to ensure type check failures here and not an access // copy map to ensure type check failures here and not an access
return Map.from(value.cast<A, B>()); return Map.from(value.cast<A, B>());
} catch (_) { } catch (_) {
Logs().w( Logs().v(
'Unable to create "Map<$A,$B>" in event content for the key "$key"'); 'Unable to create "Map<$A,$B>" in event content for the key "$key" at ${StackTrace.current.firstLine}');
return null; 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)';
}
}

View File

@ -26,7 +26,7 @@ import 'package:test/test.dart';
void main() { void main() {
group('Try-get-map-extension', () { group('Try-get-map-extension', () {
Logs().level = Level.error; Logs().level = Level.verbose;
test('it should work', () { test('it should work', () {
final data = <String, dynamic>{ final data = <String, dynamic>{
'str': 'foxies', 'str': 'foxies',
@ -40,12 +40,12 @@ void main() {
expect(data.tryGet<int>('str'), null); expect(data.tryGet<int>('str'), null);
expect(data.tryGet<int>('int'), 42); expect(data.tryGet<int>('int'), 42);
expect(data.tryGet<List>('list'), [2, 3, 4]); 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'); 'boop');
expect(data.tryGet<Map<String, dynamic>>('map')?.tryGet<String>('meep'), expect(
null); data.tryGetMap<String, dynamic>('map')?.tryGet<String>('meep'), null);
expect(data.tryGet<Map<String, dynamic>>('pam')?.tryGet<String>('beep'), expect(
null); data.tryGetMap<String, dynamic>('pam')?.tryGet<String>('beep'), null);
}); });
}); });
} }