stride3d/stride · error · YamlException

ex.Message

Error message

ex.Message

What it means

When deserialization of the current YAML node fails with an unexpected exception, Serializer.Deserialize unwraps the inner exception and rethrows it as YamlException wrapping the failing node and original exception. This is the library's generic deserialization failure: the YAML structure could not be converted into the expected type.

Solutions

  1. Inspect the YamlException's inner exception and node path to find the offending YAML element.
  2. Fix the YAML document to match the target type's members and value types.
  3. Add [YamlMember(Alias=...)] or a custom serializer for renamed/changed members.
  4. Make migration code handle old schema versions before deserializing.
  5. Ensure constructors/property setters used during deserialization don't throw for valid deserialized states.

Example fix

// before
var obj = serializer.Deserialize(reader, typeof(MyData)); // YamlException on unknown member
// after
try
{
    var obj = serializer.Deserialize(reader, typeof(MyData));
}
catch (YamlException ex)
{
    throw new InvalidOperationException($"YAML error at {ex.Node?.Start}: {ex.InnerException?.Message}", ex);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-validate document shape where possible
if (yamlText.Contains("$type") && !allowPolymorphism) throw new InvalidDataException("Unexpected $type reference");

Type guard

static bool LooksLikeYamlObject(string s) => s != null && s.TrimStart().StartsWith("{") || (s != null && s.TrimStart().StartsWith("-")) || (s != null && s.Contains(':'));

Try / catch

try { return serializer.Deserialize(reader, expectedType, existingObject, settings, out ctx); }
catch (YamlException ex)
{
    // ex.Node points at the failing element; ex.InnerException has the root cause
    throw new AssetLoadException($"YAML deserialization failed at {ex.Node?.Start}", ex);
}
catch (Exception ex) when (ex is not YamlException) { /* already rethrown as YamlException by the library */ throw; }

Prevention

When it happens

Trigger: Any exception thrown inside object graph construction during Deserialize — e.g. a property setter threw, a member value in YAML didn't match the target type, a constructor threw, or a custom serializer failed. Non-YamlExceptions are caught, unwrapped, and rethrown as YamlException(node, ex).

Common situations: YAML documents from older/newer asset versions whose fields no longer match the C# classes; typos in member names; type mismatches (string where int expected); exceptions inside object constructors during deserialization.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/eb956b02f9146a10. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/Serialization/Serializer.cs:530

            object result = null;
            if (!reader.Accept<DocumentEnd>() && !reader.Accept<StreamEnd>())
            {
                context = new SerializerContext(this, contextSettings) {Reader = reader};
                var node = context.Reader.Parser.Current;
                try
                {
                    var objectContext = new ObjectContext(context, existingObject, context.FindTypeDescriptor(expectedType));
                    result = context.Serializer.ObjectSerializer.ReadYaml(ref objectContext);
                }
                catch (YamlException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    ex = ex.Unwrap();
                    throw new YamlException(node, ex);
                }
            }

            if (hasDocumentStart)
            {
                reader.Expect<DocumentEnd>();
            }

            if (hasStreamStart)
            {
                reader.Expect<StreamEnd>();
            }

            return result;
        }

        public IYamlSerializable GetSerializer(SerializerContext context, ITypeDescriptor typeDescriptor)
        {

View on GitHub (pinned to 96fad776d2)