Skip to main content

spacetimedb_schema/def/
error.rs

1use crate::identifier::Identifier;
2use crate::relation::{FieldName, Header};
3use crate::table_name::TableName;
4use derive_more::Display;
5use spacetimedb_lib::db::raw_def::IndexType;
6use spacetimedb_primitives::{ColId, ColList};
7use spacetimedb_sats::product_value::InvalidFieldError;
8use spacetimedb_sats::raw_identifier::RawIdentifier;
9use spacetimedb_sats::satn::Satn as _;
10use spacetimedb_sats::{buffer, AlgebraicType, AlgebraicValue};
11use std::fmt;
12use std::string::FromUtf8Error;
13use thiserror::Error;
14
15#[derive(Error, Debug)]
16pub enum TypeError {
17    #[error("The type of `{{value.to_satns()}}` cannot be inferred")]
18    CannotInferType { value: AlgebraicValue },
19}
20
21#[derive(Error, Debug, Clone)]
22pub enum DecodeError {
23    #[error("Decode UTF8: {0}")]
24    Utf8(#[from] FromUtf8Error),
25    #[error("AlgebraicType::decode: Unknown: {0}")]
26    AlgebraicTypeUnknown(u8),
27    #[error("AlgebraicType::decode: Byte array has invalid length: {0:?}")]
28    AlgebraicType(usize),
29    #[error("SumType::decode: Byte array has invalid length: {0:?}")]
30    SumType(usize),
31    #[error("ProductType::decode: Byte array has invalid length: {0:?}")]
32    ProductType(usize),
33    #[error("ProductTypeElement::decode: Byte array has invalid length: {0:?}")]
34    ProductTypeElement(usize),
35    #[error("AlgebraicValue::decode: byte array length not long enough to decode {0:?}")]
36    AlgebraicValue(AlgebraicType),
37    #[error("AlgebraicValue::decode: byte array length not long enough to get length of {0:?}")]
38    AlgebraicValueGetLength(AlgebraicType),
39    #[error(
40    "AlgebraicValue::decode: buffer has no room to decode any more elements from this {kind:?}. (len: {len} <= read:{read})"
41    )]
42    AlgebraicValueRoom {
43        kind: AlgebraicType,
44        len: usize,
45        read: usize,
46    },
47    #[error("AlgebraicValue::decode: Cannot decode {kind:?}, buffer not long enough. (len: {len}, read:{read})")]
48    TypeBufferSmall {
49        kind: AlgebraicType,
50        len: usize,
51        read: usize,
52    },
53    #[error(
54        "AlgebraicValue::decode: byte array length not long enough to decode {kind:?}. (expect: {expect}, read:{read})"
55    )]
56    TypeTooSmall {
57        kind: AlgebraicType,
58        expect: usize,
59        read: usize,
60    },
61    #[error("EnumValue::decode: Byte array length is invalid.")]
62    EnumValue,
63}
64
65#[derive(Error, Debug, Clone)]
66pub enum LibError {
67    #[error("DecodeError: {0}")]
68    Decode(#[from] DecodeError),
69    #[error("BufferError: {0}")]
70    Buffer(#[from] buffer::DecodeError),
71    #[error(transparent)]
72    TupleFieldInvalid(#[from] InvalidFieldError),
73}
74
75#[derive(thiserror::Error, Debug)]
76pub enum AuthError {
77    #[error("Table `{named}` is private")]
78    TablePrivate { named: String },
79    #[error("Index `{named}` is private")]
80    IndexPrivate { named: String },
81    #[error("Sequence `{named}` is private")]
82    SequencePrivate { named: String },
83    #[error("Insufficient privileges to perform the requested operation")]
84    InsuffientPrivileges,
85    #[error("Constraint `{named}` is private")]
86    ConstraintPrivate { named: String },
87}
88
89#[derive(thiserror::Error, Debug)]
90pub enum RelationError {
91    #[error("Field `{1}` not found. Must be one of {0}")]
92    FieldNotFound(Header, FieldName),
93    #[error("Field `{0}` fail to infer the type: {1}")]
94    TypeInference(FieldName, TypeError),
95    #[error("Field with value `{}` was not a `bool`", val.to_satn())]
96    NotBoolValue { val: AlgebraicValue },
97    #[error("Field `{field}` was expected to be `bool` but is `{}`", ty.to_satn())]
98    NotBoolType { field: FieldName, ty: AlgebraicType },
99    #[error("Field declaration only support `table.field` or `field`. It gets instead `{0}`")]
100    FieldPathInvalid(String),
101}
102
103#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Display)]
104pub enum DefType {
105    Column,
106    Index,
107    Sequence,
108    Constraint,
109}
110
111#[derive(thiserror::Error, Debug, PartialEq)]
112pub enum SchemaError {
113    #[error("Multiple primary columns defined for table: {table} columns: {pks:?}")]
114    MultiplePrimaryKeys { table: Box<str>, pks: Vec<String> },
115    #[error("{ty} {name} columns `{columns:?}` not found  in table `{table}`")]
116    ColumnsNotFound {
117        name: RawIdentifier,
118        table: TableName,
119        columns: Vec<ColId>,
120        ty: DefType,
121    },
122    #[error("table `{table}` {ty} should have name. {ty} id: {id}")]
123    EmptyName { table: TableName, ty: DefType, id: u32 },
124    #[error("table `{table}` have `Constraints::unset()` for columns: {columns:?}")]
125    ConstraintUnset {
126        table: TableName,
127        name: RawIdentifier,
128        columns: ColList,
129    },
130    #[error("Attempt to define a column with more than 1 auto_inc sequence: Table: `{table}`, Field: `{field}`")]
131    OneAutoInc { table: TableName, field: Identifier },
132    #[error("Only Btree Indexes are supported: Table: `{table}`, Index: `{index}` is a `{index_type}`")]
133    OnlyBtree {
134        table: TableName,
135        index: RawIdentifier,
136        index_type: IndexType,
137    },
138}
139
140#[derive(thiserror::Error, Debug, PartialEq)]
141pub struct SchemaErrors(pub Vec<SchemaError>);
142
143impl fmt::Display for SchemaErrors {
144    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145        f.debug_list().entries(self.0.iter()).finish()
146    }
147}