Skip to content

Commit 34ac2ae

Browse files
committed
chore: improve error logging
1 parent c192e6c commit 34ac2ae

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

bin/script_runner.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import 'package:script_runner/utils.dart';
66
/// Main entrypoint for CMD script runner.
77
Future<void> main(List<String> args) async {
88
if (args.isEmpty) {
9-
printColor('No script command provided. Use -h to see available commands.', [TerminalColor.red]);
9+
printColor('No script command provided. Use -h or -ls to see available commands.',
10+
[TerminalColor.red]);
1011
return;
1112
}
1213
final scriptCmd = args.first;
@@ -15,13 +16,15 @@ Future<void> main(List<String> args) async {
1516
final code = await runScript(scriptCmd, scriptArgs);
1617
io.exit(code);
1718
} catch (e, stack) {
18-
if (e is ScriptStateError) {
19+
if (e is ScriptError) {
1920
printColor(e.toString(), [TerminalColor.red]);
20-
} else {
21-
printColor('$e\n$stack', [TerminalColor.red]);
22-
}
23-
if (e is io.ProcessException) {
21+
} else if (e is io.ProcessException) {
22+
printColor('Error in script "$scriptCmd": ${e.message}', [TerminalColor.red]);
2423
io.exit(e.errorCode);
24+
} else {
25+
printColor('Error executing script: $e\n$stack', [TerminalColor.red]);
26+
io.exit(1);
2527
}
2628
}
2729
}
30+

lib/base.dart

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Future<int> runScript(String entryName, List<String> args) async {
77
final config = await ScriptRunnerConfig.get();
88

99
if (config.scripts.isEmpty) {
10-
throw ScriptStateError('No scripts found');
10+
throw ScriptNotFoundError('No scripts found');
1111
}
1212

1313
if (['-h', '--help'].contains(entryName)) {
@@ -24,27 +24,33 @@ Future<int> runScript(String entryName, List<String> args) async {
2424
final entry = config.scriptsMap[entryName];
2525

2626
if (entry == null) {
27-
final suggestions =
28-
config.scriptsMap.keys.where((key) => key.toLowerCase().startsWith(entryName.toLowerCase())).toList();
27+
final suggestions = config.scriptsMap.keys
28+
.where((key) => key.toLowerCase().startsWith(entryName.toLowerCase()))
29+
.toList();
2930

3031
if (suggestions.isNotEmpty) {
3132
if (suggestions.length == 1) {
32-
throw ScriptStateError(
33+
throw ScriptNotFoundError(
3334
'No script named "$entryName" found. Did you mean "${suggestions.single}"?',
3435
);
3536
} else {
36-
throw ScriptStateError(
37+
throw ScriptNotFoundError(
3738
'No script named "$entryName" found.\n'
3839
'Did you mean one of: "${suggestions.join('", "')}"?',
3940
);
4041
}
4142
} else {
42-
throw ScriptStateError(
43+
throw ScriptNotFoundError(
4344
'No script named "$entryName" found.\n'
4445
'Available scripts: ${config.scriptsMap.keys.join('", "')}',
4546
);
4647
}
4748
}
4849

49-
return entry.run(args);
50+
try {
51+
return entry.run(args);
52+
} catch (e, stack) {
53+
throw ScriptError('Error running script "$entryName": $e\n$stack');
54+
}
5055
}
56+

lib/utils.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,20 @@ class TerminalColor {
135135
static const TerminalColor underline = TerminalColor._(4);
136136
}
137137

138-
class ScriptStateError extends StateError {
139-
ScriptStateError(super.message);
138+
/// An error that occurs that is related to a script.
139+
class ScriptError extends StateError {
140+
ScriptError(super.message);
140141

141142
@override
142143
String toString() => message;
143144
}
145+
146+
/// An error that occurs during script execution.
147+
class ScriptStateError extends ScriptError {
148+
ScriptStateError(super.message);
149+
}
150+
151+
/// An error that occurs when a script is not found.
152+
class ScriptNotFoundError extends ScriptError {
153+
ScriptNotFoundError(super.message);
154+
}

pubspec.yaml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ dev_dependencies:
2020
btool: any
2121

2222
script_runner:
23-
# line_length: 100
2423
scripts:
24+
# ================================================================================
2525
# Real
26+
# ================================================================================
2627
- auto-fix: dart fix --apply
27-
- publish: dart format .; dart pub publish; format
28+
- publish: dart format .; dart pub publish
2829
- publish:dry: dart pub publish --dry-run
2930
- doc: dart doc
3031
- name: version
@@ -33,15 +34,13 @@ script_runner:
3334
- name: 'version:set'
3435
cmd: dart run btool set packageVersion
3536
display_cmd: false
36-
- format: dart format --line-length 120 .
37-
- name: clean
38-
cmd: rm -rf .dart_tool/pub/bin/script_runner/script_runner.dart-*.snapshot
39-
- name: activate-local
40-
cmd: scr clean && dart pub global deactivate script_runner; dart pub global activate --source path ./
41-
- name: activate-global
42-
cmd: scr clean && dart pub global deactivate script_runner; dart pub global activate script_runner
37+
- clean: rm -rf .dart_tool/pub/bin/script_runner/script_runner.dart-*.snapshot
38+
- activate-local: scr clean && dart pub global deactivate script_runner; dart pub global activate --source path ./
39+
- activate-global: scr clean && dart pub global deactivate script_runner; dart pub global activate script_runner
4340

41+
# ================================================================================
4442
# Examples
43+
# ================================================================================
4544
- name: echo1
4645
cmd: echo "Hello World" $SHELL
4746
description: Interdum a scelerisque arcu felis taciti ligula pellentesque curabitur, suspendisse adipiscing quisque sed luctus elementum in imperdiet id, praesent enim sem justo sapien diam nec. Quisque erat risus sagittis penatibus per, vehicula sociosqu cubilia convallis, sollicitudin scelerisque cras aptent. Natoque ornare dictumst netus litora mollis suspendisse cubilia proin morbi primis consequat eu massa, cursus non urna ridiculus dolor duis tempus ut nam velit lacus viverra. A interdum senectus eu mus leo aptent facilisi augue tristique ante purus condimentum pulvinar porta viverra morbi, et tellus gravida porttitor non euismod suscipit neque egestas praesent arcu luctus pharetra fusce. Luctus mauris a venenatis tempus cras ante efficitur massa ultricies mollis lacus, volutpat nisi lacinia himenaeos facilisi in aliquet sodales purus integer vitae quisque, libero torquent enim mattis placerat tortor mi dignissim viverra sem.

0 commit comments

Comments
 (0)