Skip to main content

systemprompt_extension/
error.rs

1//! Typed error enums raised by extension registration and configuration.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use thiserror::Error;
7
8#[derive(Debug, Error)]
9pub enum LoaderError {
10    #[error("Extension '{extension}' requires dependency '{dependency}' which is not registered")]
11    MissingDependency {
12        extension: String,
13        dependency: String,
14    },
15
16    #[error("Extension with ID '{0}' is already registered")]
17    DuplicateExtension(String),
18
19    #[error("Extension '{0}' is required and cannot be disabled")]
20    RequiredExtensionDisabled(String),
21
22    #[error(
23        "Extension '{dependency}' is disabled but extension '{extension}' depends on it; disable \
24         '{extension}' as well or re-enable '{dependency}'"
25    )]
26    DisabledDependency {
27        extension: String,
28        dependency: String,
29    },
30
31    #[error("Failed to initialize extension '{extension}': {message}")]
32    InitializationFailed { extension: String, message: String },
33
34    #[error("Failed to install schema for extension '{extension}': {message}")]
35    SchemaInstallationFailed { extension: String, message: String },
36
37    #[error("Migration failed for extension '{extension}': {message}")]
38    MigrationFailed { extension: String, message: String },
39
40    #[error(
41        "Migration {version} for extension '{extension}' is not reversible (no down SQL provided)"
42    )]
43    MigrationNotReversible { extension: String, version: u32 },
44
45    #[error(
46        "Extension '{extension}' migration slot {version} was applied as '{stored_name}' but the \
47         tree now names it '{current_name}'. A migration file was deleted and its number reused; \
48         established databases have already spent that slot, so the new SQL would be skipped, \
49         never executed. Renumber it above every used slot and leave a \
50         `{version:03}_{stored_name}.tombstone` behind so the number cannot be claimed again."
51    )]
52    MigrationSlotReused {
53        extension: String,
54        version: u32,
55        stored_name: String,
56        current_name: String,
57    },
58
59    #[error("Configuration validation failed for extension '{extension}': {message}")]
60    ConfigValidationFailed { extension: String, message: String },
61
62    #[error("Extension '{extension}' uses reserved API path '{path}'")]
63    ReservedPathCollision { extension: String, path: String },
64
65    #[error(
66        "Extension '{extension}' has invalid base path '{path}': must be / or start with /api/"
67    )]
68    InvalidBasePath { extension: String, path: String },
69
70    #[error("Dependency cycle detected while ordering extensions: {chain}")]
71    DependencyCycle { chain: String },
72
73    #[error(
74        "Extension '{extension}' migration ALTERs table '{table}' but does not create it in its \
75         schemas() nor declare it in cross_extension_tables(); cross-extension table mutations \
76         must be declared explicitly"
77    )]
78    CrossExtensionAlterUndeclared { extension: String, table: String },
79
80    #[error(
81        "Extension '{extension}' migration {migration} references {kind} '{object}' via {how}, \
82         which only a declarative schema file creates; the dependent phase runs after \
83         migrations, so a database that has not booted on that schema fails here. Create it in \
84         a migration, guard the reference in a DO $$ block that tests pg_trigger/pg_views, or \
85         leave it to the declarative schema"
86    )]
87    MigrationReferencesDeclarativeObject {
88        extension: String,
89        migration: String,
90        kind: String,
91        object: String,
92        how: String,
93    },
94
95    #[error(
96        "Table '{table}' is created by both extension '{extension_a}' and '{extension_b}'; every \
97         table must be declared by exactly one extension"
98    )]
99    DuplicateTableOwner {
100        table: String,
101        extension_a: String,
102        extension_b: String,
103    },
104
105    #[error(
106        "Extension '{extension}' declares cross_extension_tables() entry '{table}', which is not \
107         a table created by any other loaded extension"
108    )]
109    CrossExtensionTableNotOwned { extension: String, table: String },
110
111    #[error(
112        "Extension '{extension}' seed '{seed}' contains forbidden statement '{statement}'; seeds \
113         may only contain INSERT … ON CONFLICT, UPDATE, MERGE, or WITH … INSERT"
114    )]
115    InvalidSeedStatement {
116        extension: String,
117        seed: String,
118        statement: String,
119    },
120
121    #[error(
122        "Extension '{extension}' seed '{seed}' contains a bare INSERT with no ON CONFLICT clause; \
123         seeds run on every boot and must be idempotent — add ON CONFLICT … DO NOTHING/UPDATE"
124    )]
125    SeedInsertNotIdempotent { extension: String, seed: String },
126
127    #[error("Extension '{extension}' seed '{seed}' failed to parse or apply: {message}")]
128    SeedFailed {
129        extension: String,
130        seed: String,
131        message: String,
132    },
133}
134
135#[derive(Debug, Error)]
136pub enum ExtensionConfigError {
137    #[error("Configuration key '{0}' not found")]
138    NotFound(String),
139
140    #[error("Invalid configuration value for '{key}': {message}")]
141    InvalidValue { key: String, message: String },
142
143    #[error("Failed to parse configuration: {message}")]
144    ParseError { message: String },
145
146    #[error("Schema validation failed: {0}")]
147    SchemaValidation(String),
148}