Skip to content

Commit dfb916d

Browse files
committed
Remove on_success from DefaultHandlers
The idea is to force the users to be explicit on which state is to be used on a successful action. Using the terminal successful test being likely an error on most cases. Signed-off-by: Didier Wenzek <didier.wenzek@free.fr>
1 parent 6186d8b commit dfb916d

File tree

3 files changed

+7
-23
lines changed

3 files changed

+7
-23
lines changed

crates/core/tedge_api/src/workflow/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub enum ScriptDefinitionError {
3737

3838
#[error("Invalid exit code range '{from}-{to}' as {from}>{to}")]
3939
IncorrectRange { from: u8, to: u8 },
40+
41+
#[error("No handler is provided for 'on_success'")]
42+
MissingOnSuccessHandler,
4043
}
4144

4245
/// Error related to state excerpt definitions

crates/core/tedge_api/src/workflow/script.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,21 +346,18 @@ impl IterateHandlers {
346346
#[derive(Clone, Debug, Eq, PartialEq)]
347347
pub struct DefaultHandlers {
348348
pub timeout: Option<Duration>,
349-
pub on_success: GenericStateUpdate,
350349
pub on_error: GenericStateUpdate,
351350
pub on_timeout: GenericStateUpdate,
352351
}
353352

354353
impl DefaultHandlers {
355354
pub fn new(
356355
timeout: Option<Duration>,
357-
on_success: Option<GenericStateUpdate>,
358356
on_error: Option<GenericStateUpdate>,
359357
on_timeout: Option<GenericStateUpdate>,
360358
) -> Self {
361359
DefaultHandlers {
362360
timeout,
363-
on_success: on_success.unwrap_or_else(GenericStateUpdate::successful),
364361
on_error: on_error.unwrap_or_else(GenericStateUpdate::unknown_error),
365362
on_timeout: on_timeout.unwrap_or_else(GenericStateUpdate::timeout),
366363
}
@@ -371,7 +368,6 @@ impl Default for DefaultHandlers {
371368
fn default() -> Self {
372369
DefaultHandlers {
373370
timeout: None,
374-
on_success: GenericStateUpdate::successful(),
375371
on_error: GenericStateUpdate::unknown_error(),
376372
on_timeout: GenericStateUpdate::timeout(),
377373
}
@@ -664,7 +660,6 @@ on_exit._ = "oops"
664660
handlers_from_toml_with_defaults("", ""),
665661
handlers_from_toml(
666662
r#"
667-
on_success = "successful"
668663
on_timeout = { "status" = "failed", reason = "timeout" }
669664
on_error = "failed"
670665
"#
@@ -683,12 +678,11 @@ on_error = "failed"
683678
);
684679

685680
assert_eq!(
686-
handlers_from_toml_with_defaults(r#"on_success = "ok""#, ""),
681+
handlers_from_toml_with_defaults(r#"on_error = "broken""#, ""),
687682
handlers_from_toml(
688683
r#"
689-
on_success = "ok"
690684
on_timeout = { "status" = "failed", reason = "timeout" }
691-
on_error = "failed"
685+
on_error = "broken"
692686
"#
693687
),
694688
);

crates/core/tedge_api/src/workflow/toml_config.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,6 @@ impl TryFrom<(TomlExitHandlers, DefaultHandlers)> for ExitHandlers {
275275
})
276276
.collect();
277277

278-
// Inject defaults if no success state is given either with: on_success, on_exit.0, on_return
279-
let on_success = if on_success.is_none()
280-
&& on_stdout.is_empty()
281-
&& !on_exit.iter().any(|(i, _, _)| *i == 0)
282-
{
283-
Some(defaults.on_success)
284-
} else {
285-
on_success
286-
};
287-
288278
// Inject defaults if no error state is given either with: on_error, on_exit._
289279
let on_error = if on_error.is_none() && wildcard.is_none() {
290280
Some(defaults.on_error)
@@ -327,7 +317,7 @@ impl TryFrom<(TomlExitHandlers, DefaultHandlers)> for AwaitHandlers {
327317
let on_success: GenericStateUpdate = handlers
328318
.on_success
329319
.map(|u| u.into())
330-
.unwrap_or(defaults.on_success);
320+
.ok_or(ScriptDefinitionError::MissingOnSuccessHandler)?;
331321
let on_error = handlers
332322
.on_error
333323
.map(|u| u.into())
@@ -372,13 +362,10 @@ impl TryFrom<TomlExitHandlers> for DefaultHandlers {
372362

373363
fn try_from(value: TomlExitHandlers) -> Result<Self, Self::Error> {
374364
let timeout = value.timeout_second.map(Duration::from_secs);
375-
let on_success = value.on_success.map(|u| u.into());
376365
let on_timeout = value.on_timeout.map(|u| u.into());
377366
let on_error = value.on_error.map(|u| u.into());
378367

379-
Ok(DefaultHandlers::new(
380-
timeout, on_success, on_error, on_timeout,
381-
))
368+
Ok(DefaultHandlers::new(timeout, on_error, on_timeout))
382369
}
383370
}
384371

0 commit comments

Comments
 (0)