LINE1 10 20 30 40 50 60 70 80 90 100 110 1201use std::{error::Error, fmt};23use crate::error::ContextError;45#[derive(Debug)]6pub enum OnceError {7 Docker(String),8 InvalidNamespace(String),9 InvalidMetadata(String),10 ApplicationNotFound(String),11 HostConflict(String),12 ServerProtected,13 ApplicationNotRunning(String),14 ReservedEnvironment(String),15 CommandExit(i32),16 Io(String),17 InvalidBackup(String),18 Context(ContextError),19}2021impl fmt::Display for OnceError {22 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {23 match self {24 Self::Docker(message) => write!(formatter, "Docker: {message}"),25 Self::InvalidNamespace(name) => write!(26 formatter,27 "invalid namespace {name:?}: use lowercase letters, digits, and hyphens, starting with a letter or digit"28 ),29 Self::InvalidMetadata(message) => write!(formatter, "invalid Once metadata: {message}"),30 Self::ApplicationNotFound(host) => {31 write!(formatter, "no application found at host {host:?}")32 }33 Self::HostConflict(host) => write!(formatter, "hostname already in use: {host}"),34 Self::ServerProtected => write!(35 formatter,36 "the server app cannot be controlled through its own API"37 ),38 Self::ApplicationNotRunning(host) => {39 write!(formatter, "application at host {host:?} is not running")40 }41 Self::ReservedEnvironment(name) => write!(42 formatter,43 "environment variable {name} is managed by Ino and cannot be set"44 ),45 Self::CommandExit(code) => write!(formatter, "command exited with status {code}"),46 Self::Io(message) => write!(formatter, "I/O: {message}"),47 Self::InvalidBackup(message) => write!(formatter, "invalid backup archive: {message}"),48 Self::Context(error) => write!(formatter, "{error}"),49 }50 }51}5253impl Error for OnceError {54 fn source(&self) -> Option<&(dyn Error + 'static)> {55 match self {56 Self::Context(error) => Some(error),57 _ => None,58 }59 }60}6162impl OnceError {63 pub fn context<E>(context: impl Into<String>, source: E) -> Self64 where65 E: Error + Send + Sync + 'static,66 {67 Self::Context(ContextError::new(context, source))68 }6970 pub fn exit_code(&self) -> Option<i32> {71 match self {72 Self::CommandExit(code) => Some(*code),73 _ => None,74 }75 }76}7778impl From<bollard::errors::Error> for OnceError {79 fn from(error: bollard::errors::Error) -> Self {80 Self::Docker(error.to_string())81 }82}8384impl From<serde_json::Error> for OnceError {85 fn from(error: serde_json::Error) -> Self {86 Self::InvalidMetadata(error.to_string())87 }88}8990impl From<std::io::Error> for OnceError {91 fn from(error: std::io::Error) -> Self {92 Self::Io(error.to_string())93 }94}