From d9a55ad278a108d3b7df8509bb2ac8b055f50c38 Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Sat, 28 Feb 2026 17:50:08 +0100 Subject: [PATCH] Migrate --on-error to --if-error. Since the changes are only in the CLI (which uses a recent SDK for compilation) we don't need to worry about older SDKs. --- src/cli/cmds/device-container.toit | 4 ++-- src/cli/cmds/device.toit | 4 ++-- src/cli/cmds/fleet.toit | 4 ++-- src/cli/cmds/serial.toit | 2 +- src/cli/pod-registry.toit | 18 +++++++++++------- src/cli/pod-specification.toit | 2 +- src/cli/utils/utils.toit | 24 ++++++++++++------------ tests/parse-duration-test.toit | 4 ++-- tests/pod-designation-parse-test.toit | 18 +++++++++--------- 9 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/cli/cmds/device-container.toit b/src/cli/cmds/device-container.toit index 6d58b9e1..9e73737d 100644 --- a/src/cli/cmds/device-container.toit +++ b/src/cli/cmds/device-container.toit @@ -100,7 +100,7 @@ install-container invocation/Invocation: if seen-triggers.contains "interval": ui.abort "Duplicate trigger 'interval'." seen-triggers.add "interval" - duration := parse-duration parsed-trigger["interval"] --on-error=: + duration := parse-duration parsed-trigger["interval"] --if-error=: ui.abort "Invalid interval '$parsed-trigger'. Use 20s, 5m10s, 12h or similar." triggers.add (pod-specification.IntervalTrigger duration) else if parsed-trigger is Map and (parsed-trigger.contains "gpio-low" or parsed-trigger.contains "gpio-high"): @@ -108,7 +108,7 @@ install-container invocation/Invocation: seen-triggers.add "gpio" on-high := parsed-trigger.contains "gpio-high" pin-string := on-high ? parsed-trigger["gpio-high"] : parsed-trigger["gpio-low"] - pin := int.parse pin-string --on-error=: + pin := int.parse pin-string --if-error=: ui.abort "Invalid pin '$pin-string'." if seen-pins.contains pin: diff --git a/src/cli/cmds/device.toit b/src/cli/cmds/device.toit index 7144f0b7..5e892448 100644 --- a/src/cli/cmds/device.toit +++ b/src/cli/cmds/device.toit @@ -368,9 +368,9 @@ set-max-offline invocation/Invocation: ui := invocation.cli.ui with-device invocation: | device/DeviceFleet fleet/FleetWithDevices | - max-offline-seconds := int.parse max-offline --on-error=: + max-offline-seconds := int.parse max-offline --if-error=: // Assume it's a duration with units, like "5s". - duration := parse-duration max-offline --on-error=: + duration := parse-duration max-offline --if-error=: ui.abort "Invalid max-offline duration: $max-offline." duration.in-s diff --git a/src/cli/cmds/fleet.toit b/src/cli/cmds/fleet.toit index 9b0f0f83..45b3d91b 100644 --- a/src/cli/cmds/fleet.toit +++ b/src/cli/cmds/fleet.toit @@ -831,7 +831,7 @@ group-add invocation/Invocation: with-devices-fleet invocation: | fleet/FleetWithDevices | pod-reference/PodReference? := null if pod: - pod-reference = PodReference.parse pod --on-error=: + pod-reference = PodReference.parse pod --if-error=: ui.abort "Invalid pod reference: $pod" else if template: pod-reference = fleet.pod-reference-for-group template @@ -877,7 +877,7 @@ group-update invocation/Invocation: pod-reference/PodReference? := null if pod: - pod-reference = PodReference.parse pod --on-error=: + pod-reference = PodReference.parse pod --if-error=: ui.abort "Invalid pod reference: $pod" groups.do: | group/string | if not fleet-file.group-pods.contains group: diff --git a/src/cli/cmds/serial.toit b/src/cli/cmds/serial.toit index d04c8e19..c689c0f7 100644 --- a/src/cli/cmds/serial.toit +++ b/src/cli/cmds/serial.toit @@ -157,7 +157,7 @@ build-partitions_ partition-list/List --cli/Cli -> List: ui.emit --error "No such file: $value." ui.abort else: - size := int.parse value --on-error=: + size := int.parse value --if-error=: ui.abort "Partition '$type:$name' has illegal size: '$it'." if size <= 0: ui.abort "Partition '$type:$name' has illegal size: $size." diff --git a/src/cli/pod-registry.toit b/src/cli/pod-registry.toit index 5dd18914..0792785b 100644 --- a/src/cli/pod-registry.toit +++ b/src/cli/pod-registry.toit @@ -34,23 +34,25 @@ class PodReference: static parse str/string --allow-name-only/bool=false --cli/Cli -> PodReference: return parse str --allow-name-only=allow-name-only - --on-error=(: cli.ui.abort it) + --if-error=(: cli.ui.abort it) - static parse str/string --allow-name-only/bool=false [--on-error] -> PodReference: + static parse str/string --allow-name-only/bool=false [--if-error] -> PodReference: name/string? := null revision/int? := null tag/string? := null hash-index := str.index-of "#" at-index := str.index-of "@" if hash-index >= 0 and at-index >= 0: - on-error.call "Cannot specify both revision and tag: '$str'." + if-error.call "Cannot specify both revision and tag: '$str'." + unreachable if hash-index >= 0: if revision: - on-error.call "Cannot specify the revision as option and in the name." + if-error.call "Cannot specify the revision as option and in the name." + unreachable revision-string := str[hash-index + 1..] revision = int.parse revision-string - --on-error=(: on-error.call "Invalid revision: '$revision-string'.") + --if-error=(: if-error.call "Invalid revision: '$revision-string'.") name = str[..hash-index] return PodReference --name=name --revision=revision @@ -72,11 +74,13 @@ class PodReference: if allow-name-only: name = str return PodReference --name=name - on-error.call "Invalid pod uuid: '$str'." + if-error.call "Invalid pod uuid: '$str'." + unreachable return PodReference --id=id if not allow-name-only: - on-error.call "Invalid pod reference: '$str'." + if-error.call "Invalid pod reference: '$str'." + unreachable return PodReference --name=str diff --git a/src/cli/pod-specification.toit b/src/cli/pod-specification.toit index 733f7351..8a137d0c 100644 --- a/src/cli/pod-specification.toit +++ b/src/cli/pod-specification.toit @@ -239,7 +239,7 @@ class JsonMap: entry-string = entry-string.trim if entry-string == "": return Duration.ZERO - return parse-duration entry-string --on-error=: + return parse-duration entry-string --if-error=: format-error_ "$entry-type $key in $holder is not a valid duration: $entry" get-optional-duration key/string --entry-type/string="Entry" -> Duration?: diff --git a/src/cli/utils/utils.toit b/src/cli/utils/utils.toit index 692678e8..f2b04d3d 100644 --- a/src/cli/utils/utils.toit +++ b/src/cli/utils/utils.toit @@ -78,28 +78,28 @@ read-base64-ubjson path/string -> any: return ubjson.decode (base64.decode data) read-file path/string --cli/Cli [block] -> any: - return read-file path block --on-error=: | exception | + return read-file path block --if-error=: | exception | cli.ui.abort "Failed to open '$path' for reading ($exception)." -read-file path/string [block] [--on-error] -> any: +read-file path/string [block] [--if-error] -> any: stream/file.Stream? := null exception := catch: stream = file.Stream.for-read path if not stream: - return on-error.call exception + return if-error.call exception try: return block.call stream.in finally: stream.close write-file path/string --cli/Cli [block] -> none: - write-file path block --on-error=: | exception | + write-file path block --if-error=: | exception | cli.ui.abort "Failed to open '$path' for writing ($exception)." -write-file path/string [block] [--on-error] -> none: +write-file path/string [block] [--if-error] -> none: stream/file.Stream? := null exception := catch: stream = file.Stream.for-write path if not stream: - on-error.call exception + if-error.call exception return try: block.call stream.out @@ -263,24 +263,24 @@ parse-pod-specification-file path/string --cli/Cli -> PodSpecification: Parses a string like "1h 30m 10s" or "1h30m10s" into a Duration. */ parse-duration str/string -> Duration: - return parse-duration str --on-error=: throw "Invalid duration string: $it" + return parse-duration str --if-error=: throw "Invalid duration string: $it" -parse-duration str/string [--on-error] -> Duration: +parse-duration str/string [--if-error] -> Duration: UNITS ::= ["h", "m", "s"] splits-with-missing := UNITS.map: str.index-of it splits := splits-with-missing.filter: it != -1 if splits.is-empty or not splits.is-sorted: - return on-error.call str + return if-error.call str if splits.last != str.size - 1: - return on-error.call str + return if-error.call str last-unit := -1 values := {:} splits.do: | split/int | unit := str[split] value-string := str[last-unit + 1..split] - value := int.parse value-string.trim --on-error=: - return on-error.call str + value := int.parse value-string.trim --if-error=: + return if-error.call str values[unit] = value last-unit = split diff --git a/tests/parse-duration-test.toit b/tests/parse-duration-test.toit index eebc1c78..d2628a7e 100644 --- a/tests/parse-duration-test.toit +++ b/tests/parse-duration-test.toit @@ -5,10 +5,10 @@ import expect show * import artemis.cli.utils show parse-duration parse str/string -> Duration: - return parse-duration str --on-error=: throw "Illegal" + return parse-duration str --if-error=: throw "Illegal" expect-parse-error str/string: - parse-duration str --on-error=: return + parse-duration str --if-error=: return throw "Expected parse error" expect-equals-seconds s/int duration/Duration: diff --git a/tests/pod-designation-parse-test.toit b/tests/pod-designation-parse-test.toit index 13368f2f..a5be9fb2 100644 --- a/tests/pod-designation-parse-test.toit +++ b/tests/pod-designation-parse-test.toit @@ -4,33 +4,33 @@ import artemis.cli.pod-registry show PodReference import expect show * main: - reference := PodReference.parse "foo@bar" --on-error=: throw it + reference := PodReference.parse "foo@bar" --if-error=: throw it expect-equals "foo" reference.name expect-equals "bar" reference.tag expect-throw "Cannot specify both revision and tag: 'foo@bar#baz'.": - PodReference.parse "foo@bar#baz" --on-error=: throw it + PodReference.parse "foo@bar#baz" --if-error=: throw it - reference = PodReference.parse "foo#123" --on-error=: throw it + reference = PodReference.parse "foo#123" --if-error=: throw it expect-equals "foo" reference.name expect-equals 123 reference.revision expect-throw "Invalid revision: 'bar'.": - PodReference.parse "foo#bar" --on-error=: throw it + PodReference.parse "foo#bar" --if-error=: throw it uuid-string := "01234567-89ab-cdef-0123-456789abcdef" - reference = PodReference.parse uuid-string --on-error=: throw it + reference = PodReference.parse uuid-string --if-error=: throw it expect-equals uuid-string "$reference.id" expect-throw "Invalid pod reference: 'foo'.": - PodReference.parse "foo" --on-error=: throw it + PodReference.parse "foo" --if-error=: throw it - reference = PodReference.parse "foo" --allow-name-only --on-error=: throw it + reference = PodReference.parse "foo" --allow-name-only --if-error=: throw it expect-equals "foo" reference.name expect-throw "Invalid pod reference: 'foo'.": - PodReference.parse "foo" --on-error=: throw it + PodReference.parse "foo" --if-error=: throw it - reference = PodReference.parse "foo@bar@baz" --on-error=: throw it + reference = PodReference.parse "foo@bar@baz" --if-error=: throw it expect-equals "foo" reference.name expect-equals "bar@baz" reference.tag