using System;
namespace Unity.VisualScripting
{
///
/// Throws an exception.
///
[UnitCategory("Control")]
[UnitOrder(16)]
public sealed class Throw : Unit
{
///
/// Whether a custom exception object should be specified manually.
///
[Serialize]
[Inspectable, UnitHeaderInspectable("Custom")]
[InspectorToggleLeft]
public bool custom { get; set; }
///
/// The entry point to throw the exception.
///
[DoNotSerialize]
[PortLabelHidden]
public ControlInput enter { get; private set; }
///
/// The message of the exception.
///
[DoNotSerialize]
public ValueInput message { get; private set; }
///
/// The exception to throw.
///
[DoNotSerialize]
public ValueInput exception { get; private set; }
protected override void Definition()
{
if (custom)
{
enter = ControlInput(nameof(enter), ThrowCustom);
exception = ValueInput(nameof(exception));
Requirement(exception, enter);
}
else
{
enter = ControlInput(nameof(enter), ThrowMessage);
message = ValueInput(nameof(message), string.Empty);
Requirement(message, enter);
}
}
private ControlOutput ThrowCustom(Flow flow)
{
throw flow.GetValue(exception);
}
private ControlOutput ThrowMessage(Flow flow)
{
throw new Exception(flow.GetValue(message));
}
}
}