use js_sys::Object;
use thiserror::Error;
use wasm_bindgen::{JsCast, JsValue};
#[derive(Debug, Error, PartialEq)]
pub enum Error {
#[error("failed to add a value: {}", js_object_display(.0))]
AddFailed(JsValue),
#[error("failed to clear object store: {}", js_object_display(.0))]
ClearFailed(JsValue),
#[error("failed to get count of records: {}", js_object_display(.0))]
CountFailed(JsValue),
#[error("failed to advance cursor: {}", js_object_display(.0))]
CursorAdvanceFailed(JsValue),
#[error("failed to continue cursor: {}", js_object_display(.0))]
CursorContinueFailed(JsValue),
#[error("cursor is finished")]
CursorFinished,
#[error("failed to get cursor key: {}", js_object_display(.0))]
CursorKeyNotFound(JsValue),
#[error("failed to get cursor primary key: {}", js_object_display(.0))]
CursorPrimaryKeyNotFound(JsValue),
#[error("failed to get cursor value: {}", js_object_display(.0))]
CursorValueNotFound(JsValue),
#[error("failed to delete a value: {}", js_object_display(.0))]
DeleteFailed(JsValue),
#[error("DOM exception: {}", js_object_display(.0))]
DomException(web_sys::DomException),
#[error("DOM exception not found")]
DomExceptionNotFound,
#[error("failed to get event target")]
EventTargetNotFound,
#[error("failed to get all values: {}", js_object_display(.0))]
GetAllFailed(JsValue),
#[error("failed to get all keys: {}", js_object_display(.0))]
GetAllKeysFailed(JsValue),
#[error("failed to get a value: {}", js_object_display(.0))]
GetFailed(JsValue),
#[error("failed to get a key: {}", js_object_display(.0))]
GetKeyFailed(JsValue),
#[error("failed to create new index: {}", js_object_display(.0))]
IndexCreateFailed(JsValue),
#[error("failed to delete index: {}", js_object_display(.0))]
IndexDeleteFailed(JsValue),
#[error("failed to delete indexed db: {}", js_object_display(.0))]
IndexedDbDeleteFailed(JsValue),
#[error("indexed db not found")]
IndexedDbNotFound(JsValue),
#[error("failed to open indexed db: {}", js_object_display(.0))]
IndexedDbOpenFailed(JsValue),
#[error("failed to get index: {}", js_object_display(.0))]
IndexNotFound(JsValue),
#[error("invalid cursor direction")]
InvalidCursorDirection,
#[error("invalid key path of an object store")]
InvalidKeyPath,
#[error("invalid request ready state")]
InvalidReqeustReadyState,
#[error("invalid storage type")]
InvalidStorageType,
#[error("invalid transaction mode")]
InvalidTransactionMode,
#[error("failed to get key path of an object store: {}", js_object_display(.0))]
KeyPathNotFound(JsValue),
#[error("failed to get key range bound: {}", js_object_display(.0))]
KeyRangeBoundNotFound(JsValue),
#[error("failed to create key range: {}", js_object_display(.0))]
KeyRangeCreateFailed(JsValue),
#[error("failed to check if a value is included in key range: {}", js_object_display(.0))]
KeyRangeIncludesFailed(JsValue),
#[error("number conversion error")]
NumberConversionError,
#[error("failed to create new object store: {}", js_object_display(.0))]
ObjectStoreCreateFailed(JsValue),
#[error("failed to delete object store: {}", js_object_display(.0))]
ObjectStoreDeleteFailed(JsValue),
#[error("failed to get object store: {}", js_object_display(.0))]
ObjectStoreNotFound(JsValue),
#[error("failed to open cursor: {}", js_object_display(.0))]
OpenCursorFailed(JsValue),
#[error("failed to open key cursor: {}", js_object_display(.0))]
OpenKeyCursorFailed(JsValue),
#[error("failed to get request error: {}", js_object_display(.0))]
RequestErrorNotFound(JsValue),
#[error("failed to get request source: {}", js_object_display(.0))]
RequestResultNotFound(JsValue),
#[error("failed to get request source")]
RequestSourceNotFound,
#[error("failed to abort transaction: {}", js_object_display(.0))]
TransactionAbortError(JsValue),
#[error("failed to commit transaction: {}", js_object_display(.0))]
TransactionCommitError(JsValue),
#[error("failed to get transaction mode: {}", js_object_display(.0))]
TransactionModeNotFound(JsValue),
#[error("failed to open new transaction: {}", js_object_display(.0))]
TransactionOpenFailed(JsValue),
#[error("unexpected JS type. expected: {}, found: {}", .0, js_object_display(.1))]
UnexpectedJsType(&'static str, JsValue),
#[error("failed to update a value: {}", js_object_display(.0))]
UpdateFailed(JsValue),
#[cfg(feature = "futures")]
#[cfg_attr(any(docsrs, feature = "doc"), doc(cfg(feature = "futures")))]
#[error("failed to receive object on oneshot channel")]
OneshotChannelReceiveError,
#[cfg(feature = "builder")]
#[cfg_attr(any(docsrs, feature = "doc"), doc(cfg(feature = "builder")))]
#[error("no transaction associated with database request")]
TransactionNotFound,
}
fn js_object_display(option: &JsValue) -> String {
if option.is_undefined() {
"undefined".to_string()
} else if option.is_null() {
"null".to_string()
} else {
let object: &Object = option.unchecked_ref();
ToString::to_string(&object.to_string())
}
}