| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
/// This module contains glue for the FFI, including error codes and a |
| 6 |
/// `From<Error>` implementation for `ffi_support::ExternError`. These must be |
| 7 |
/// implemented in this crate, and not `webext_storage_ffi`, because of Rust's |
| 8 |
/// orphan rule for implementing traits (`webext_storage_ffi` can't implement |
| 9 |
/// a trait in `ffi_support` for a type in `webext_storage`). |
| 10 |
use ffi_support::{ErrorCode, ExternError}; |
| 11 |
|
| 12 |
use crate::error::{Error, QuotaReason}; |
| 13 |
|
| 14 |
mod error_codes { |
| 15 |
/// An unexpected error occurred which likely cannot be meaningfully handled |
| 16 |
/// by the application. |
| 17 |
pub const UNEXPECTED: i32 = 1; |
| 18 |
|
| 19 |
/// The application passed an invalid JSON string for a storage key or value. |
| 20 |
pub const INVALID_JSON: i32 = 2; |
| 21 |
|
| 22 |
/// The total number of bytes stored in the database for this extension, |
| 23 |
/// counting all key-value pairs serialized to JSON, exceeds the allowed limit. |
| 24 |
pub const QUOTA_TOTAL_BYTES_EXCEEDED: i32 = 32; |
| 25 |
|
| 26 |
/// A single key-value pair exceeds the allowed byte limit when serialized |
| 27 |
/// to JSON. |
| 28 |
pub const QUOTA_ITEM_BYTES_EXCEEDED: i32 = 32 + 1; |
| 29 |
|
| 30 |
/// The total number of key-value pairs stored for this extension exceeded the |
| 31 |
/// allowed limit. |
| 32 |
pub const QUOTA_MAX_ITEMS_EXCEEDED: i32 = 32 + 2; |
| 33 |
} |
| 34 |
|
| 35 |
impl From<Error> for ExternError { |
| 36 |
fn from(err: Error) -> ExternError { |
| 37 |
let code = ErrorCode::new(match &err { |
| 38 |
Error::JsonError(_) => error_codes::INVALID_JSON, |
| 39 |
Error::QuotaError(reason) => match reason { |
| 40 |
QuotaReason::ItemBytes => error_codes::QUOTA_ITEM_BYTES_EXCEEDED, |
| 41 |
QuotaReason::MaxItems => error_codes::QUOTA_MAX_ITEMS_EXCEEDED, |
| 42 |
QuotaReason::TotalBytes => error_codes::QUOTA_TOTAL_BYTES_EXCEEDED, |
| 43 |
}, |
| 44 |
_ => error_codes::UNEXPECTED, |
| 45 |
}); |
| 46 |
ExternError::new_error(code, err.to_string()) |
| 47 |
} |
| 48 |
} |