aws_smithy_schema/schema/serde/error.rs
1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Error types for shape serialization and deserialization.
7
8use std::fmt;
9
10/// Error type for shape serialization and deserialization operations.
11#[derive(Debug)]
12#[non_exhaustive]
13pub enum SerdeError {
14 /// The data did not match the expected type described by the schema.
15 TypeMismatch {
16 /// Description of what was expected vs what was found.
17 message: String,
18 },
19 /// A required structure member was missing during deserialization.
20 MissingMember {
21 /// The name of the missing member.
22 member_name: String,
23 },
24 /// An unknown member was encountered during deserialization.
25 UnknownMember {
26 /// The name of the unknown member.
27 member_name: String,
28 },
29 /// The input data was malformed or invalid for the format.
30 InvalidInput {
31 /// Description of the problem.
32 message: String,
33 },
34 /// The operation is not supported by this serializer or deserializer.
35 UnsupportedOperation {
36 /// Description of what was attempted.
37 message: String,
38 },
39 /// An error occurred while writing output during serialization.
40 WriteFailed {
41 /// Description of the write failure.
42 message: String,
43 },
44 /// Catch-all for errors not covered by other variants.
45 Custom {
46 /// Explanatory message.
47 message: String,
48 },
49}
50
51impl fmt::Display for SerdeError {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 match self {
54 SerdeError::TypeMismatch { message } => write!(f, "type mismatch: {message}"),
55 SerdeError::MissingMember { member_name } => {
56 write!(f, "missing required member: {member_name}")
57 }
58 SerdeError::UnknownMember { member_name } => {
59 write!(f, "unknown member: {member_name}")
60 }
61 SerdeError::InvalidInput { message } => write!(f, "invalid input: {message}"),
62 SerdeError::UnsupportedOperation { message } => {
63 write!(f, "unsupported operation: {message}")
64 }
65 SerdeError::WriteFailed { message } => write!(f, "write failed: {message}"),
66 SerdeError::Custom { message } => f.write_str(message),
67 }
68 }
69}
70
71impl std::error::Error for SerdeError {}
72
73impl SerdeError {
74 /// Creates a custom error with the given message.
75 pub fn custom(message: impl Into<String>) -> Self {
76 SerdeError::Custom {
77 message: message.into(),
78 }
79 }
80}