summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/storage/webext_storage_bridge/src/error.rs
blob: 07b14b4e1463dca57035efc8348e29d848336211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::{error, fmt, result, str::Utf8Error, string::FromUtf16Error};

use golden_gate::Error as GoldenGateError;
use nserror::{
    nsresult, NS_ERROR_ALREADY_INITIALIZED, NS_ERROR_CANNOT_CONVERT_DATA,
    NS_ERROR_DOM_QUOTA_EXCEEDED_ERR, NS_ERROR_FAILURE, NS_ERROR_INVALID_ARG,
    NS_ERROR_NOT_IMPLEMENTED, NS_ERROR_NOT_INITIALIZED, NS_ERROR_UNEXPECTED,
};
use serde_json::error::Error as JsonError;
use webext_storage::error::Error as WebextStorageError;
use webext_storage::error::ErrorKind as WebextStorageErrorKind;

/// A specialized `Result` type for extension storage operations.
pub type Result<T> = result::Result<T, Error>;

/// The error type for extension storage operations. Errors can be converted
/// into `nsresult` codes, and include more detailed messages that can be passed
/// to callbacks.
#[derive(Debug)]
pub enum Error {
    Nsresult(nsresult),
    WebextStorage(WebextStorageError),
    MigrationFailed(WebextStorageError),
    GoldenGate(GoldenGateError),
    MalformedString(Box<dyn error::Error + Send + Sync + 'static>),
    AlreadyConfigured,
    NotConfigured,
    AlreadyRan(&'static str),
    DidNotRun(&'static str),
    AlreadyTornDown,
    NotImplemented,
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Error::MalformedString(error) => Some(error.as_ref()),
            _ => None,
        }
    }
}

impl From<nsresult> for Error {
    fn from(result: nsresult) -> Error {
        Error::Nsresult(result)
    }
}

impl From<WebextStorageError> for Error {
    fn from(error: WebextStorageError) -> Error {
        Error::WebextStorage(error)
    }
}

impl From<GoldenGateError> for Error {
    fn from(error: GoldenGateError) -> Error {
        Error::GoldenGate(error)
    }
}

impl From<Utf8Error> for Error {
    fn from(error: Utf8Error) -> Error {
        Error::MalformedString(error.into())
    }
}

impl From<FromUtf16Error> for Error {
    fn from(error: FromUtf16Error) -> Error {
        Error::MalformedString(error.into())
    }
}

impl From<JsonError> for Error {
    fn from(error: JsonError) -> Error {
        Error::MalformedString(error.into())
    }
}

impl From<Error> for nsresult {
    fn from(error: Error) -> nsresult {
        match error {
            Error::Nsresult(result) => result,
            Error::WebextStorage(e) => match e.kind() {
                WebextStorageErrorKind::QuotaError(_) => NS_ERROR_DOM_QUOTA_EXCEEDED_ERR,
                _ => NS_ERROR_FAILURE,
            },
            Error::MigrationFailed(_) => NS_ERROR_CANNOT_CONVERT_DATA,
            Error::GoldenGate(error) => error.into(),
            Error::MalformedString(_) => NS_ERROR_INVALID_ARG,
            Error::AlreadyConfigured => NS_ERROR_ALREADY_INITIALIZED,
            Error::NotConfigured => NS_ERROR_NOT_INITIALIZED,
            Error::AlreadyRan(_) => NS_ERROR_UNEXPECTED,
            Error::DidNotRun(_) => NS_ERROR_UNEXPECTED,
            Error::AlreadyTornDown => NS_ERROR_UNEXPECTED,
            Error::NotImplemented => NS_ERROR_NOT_IMPLEMENTED,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::Nsresult(result) => write!(f, "Operation failed with {result}"),
            Error::WebextStorage(error) => error.fmt(f),
            Error::MigrationFailed(error) => write!(f, "Migration failed with {error}"),
            Error::GoldenGate(error) => error.fmt(f),
            Error::MalformedString(error) => error.fmt(f),
            Error::AlreadyConfigured => write!(f, "The storage area is already configured"),
            Error::NotConfigured => write!(
                f,
                "The storage area must be configured by calling `configure` first"
            ),
            Error::AlreadyRan(what) => write!(f, "`{what}` already ran on the background thread"),
            Error::DidNotRun(what) => write!(f, "`{what}` didn't run on the background thread"),
            Error::AlreadyTornDown => {
                write!(f, "Can't use a storage area that's already torn down")
            }
            Error::NotImplemented => write!(f, "Operation not implemented"),
        }
    }
}