Skip to content

Commit 9db114f

Browse files
[mustache_template] Improve error throwing (#10700)
Addresses error handling issues flagged in the Gemini review of mustache_template. Changes: - Added missing `throw` before [_error()](cci:1://file:///C:/Users/Guto_/packages-pr/third_party/packages/mustache_template/lib/src/lambda_context.dart:27:2-34:3) calls in [lambda_context.dart](cci:7://file:///C:/Users/Guto_/packages-pr/third_party/packages/mustache_template/lib/src/lambda_context.dart:0:0-0:0) - Changed `Exception` to `StateError` for unreachable code in [parser.dart](cci:7://file:///C:/Users/Guto_/packages-pr/third_party/packages/mustache_template/lib/src/parser.dart:0:0-0:0) Fixes flutter/flutter#174938 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent bab935f commit 9db114f

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

third_party/packages/mustache_template/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
## NEXT
1+
## 2.0.3
22

33
* Updates minimum supported SDK version to Flutter 3.35/Dart 3.9.
4+
* Fixes error handling in LambdaContext by adding missing throw statements.
5+
* Uses StateError instead of Exception for unreachable code in parser.
46

57
## 2.0.2
68

third_party/packages/mustache_template/lib/src/lambda_context.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ class LambdaContext implements m.LambdaContext {
3838
String renderString({Object? value}) {
3939
_checkClosed();
4040
if (_node is! SectionNode) {
41-
// TODO(stuartmorgan): Fix the lack of `throw` here, which looks like a
42-
// bug in the original code.
43-
_error(
41+
throw _error(
4442
'LambdaContext.renderString() can only be called on section tags.',
4543
);
4644
}
@@ -62,9 +60,9 @@ class LambdaContext implements m.LambdaContext {
6260
void render({Object? value}) {
6361
_checkClosed();
6462
if (_node is! SectionNode) {
65-
// TODO(stuartmorgan): Fix the lack of `throw` here, which looks like a
66-
// bug in the original code.
67-
_error('LambdaContext.render() can only be called on section tags.');
63+
throw _error(
64+
'LambdaContext.render() can only be called on section tags.',
65+
);
6866
}
6967
_renderSubtree(_renderer.sink, value);
7068
}

third_party/packages/mustache_template/lib/src/parser.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ class Parser {
9898
_parseLine();
9999

100100
default:
101-
// TODO(stuartmorgan): Convert to StateError.
102-
throw Exception('Unreachable code.');
101+
throw StateError('Unreachable code.');
103102
}
104103
}
105104

@@ -212,8 +211,7 @@ class Parser {
212211
break;
213212

214213
default:
215-
// TODO(stuartmorgan): Convert to StateError.
216-
throw Exception('Unreachable code.');
214+
throw StateError('Unreachable code.');
217215
}
218216
}
219217

@@ -412,8 +410,7 @@ class Parser {
412410
node = null;
413411

414412
default:
415-
// TODO(stuartmorgan): Convert to StateError.
416-
throw Exception('Unreachable code');
413+
throw StateError('Unreachable code.');
417414
}
418415
return node;
419416
}

third_party/packages/mustache_template/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: mustache_template
22
description: A templating library that implements the Mustache template specification
33
repository: https://github.com/flutter/packages/tree/main/third_party/packages/mustache_template
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+mustache_template%22
5-
version: 2.0.2
5+
version: 2.0.3
66

77
environment:
88
sdk: ^3.9.0

third_party/packages/mustache_template/test/mustache_test.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,29 @@ void main() {
883883
const output = '<oi!>';
884884
expect(parse(template).renderString(values), equals(output));
885885
});
886+
887+
test('LambdaContext renderString on non-section throws', () {
888+
final t = Template('{{ foo }}');
889+
expect(
890+
() => t.renderString(<String, Object>{
891+
'foo': (LambdaContext lc) => lc.renderString(),
892+
}),
893+
throwsA(isA<TemplateException>()),
894+
);
895+
});
896+
897+
test('LambdaContext render on non-section throws', () {
898+
final t = Template('{{ foo }}');
899+
expect(
900+
() => t.renderString(<String, Object>{
901+
'foo': (LambdaContext lc) {
902+
lc.render();
903+
return '';
904+
},
905+
}),
906+
throwsA(isA<TemplateException>()),
907+
);
908+
});
886909
});
887910
}
888911

0 commit comments

Comments
 (0)