| 1 | // Copyright 2023 Lance Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use arrow_schema::ArrowError; |
| 16 | use neon::context::Context; |
| 17 | use neon::prelude::NeonResult; |
| 18 | use snafu::Snafu; |
| 19 | |
| 20 | #[derive(Debug, Snafu)] |
| 21 | pub enum Error { |
| 22 | #[allow(dead_code)] |
| 23 | #[snafu(display("column '{name}' is missing"))] |
| 24 | MissingColumn { name: String }, |
| 25 | #[snafu(display("{name}: {message}"))] |
| 26 | OutOfRange { name: String, message: String }, |
| 27 | #[allow(dead_code)] |
| 28 | #[snafu(display("{index_type} is not a valid index type"))] |
| 29 | InvalidIndexType { index_type: String }, |
| 30 | |
| 31 | #[snafu(display("{message}"))] |
| 32 | LanceDB { message: String }, |
| 33 | #[snafu(display("{message}"))] |
| 34 | Neon { message: String }, |
| 35 | } |
| 36 | |
| 37 | pub type Result<T> = std::result::Result<T, Error>; |
| 38 | |
| 39 | impl From<lancedb::error::Error> for Error { |
| 40 | fn from(e: lancedb::error::Error) -> Self { |
| 41 | Self::LanceDB { |
| 42 | message: e.to_string(), |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl From<lance::Error> for Error { |
| 48 | fn from(e: lance::Error) -> Self { |
| 49 | Self::LanceDB { |
| 50 | message: e.to_string(), |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl From<ArrowError> for Error { |
| 56 | fn from(value: ArrowError) -> Self { |
| 57 | Self::LanceDB { |
| 58 | message: value.to_string(), |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl From<neon::result::Throw> for Error { |
| 64 | fn from(value: neon::result::Throw) -> Self { |
| 65 | Self::Neon { |
| 66 | message: value.to_string(), |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl<T> From<std::sync::mpsc::SendError<T>> for Error { |
| 72 | fn from(value: std::sync::mpsc::SendError<T>) -> Self { |
| 73 | Self::Neon { |
| 74 | message: value.to_string(), |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /// ResultExt is used to transform a [`Result`] into a [`NeonResult`], |
| 80 | /// so it can be returned as a JavaScript error |
| 81 | /// Copied from [Neon](https://github.com/neon-bindings/neon/blob/4c2e455a9e6814f1ba0178616d63caec7f4df317/crates/neon/src/result/mod.rs#L88) |
| 82 | pub trait ResultExt<T> { |
| 83 | fn or_throw<'a, C: Context<'a>>(self, cx: &mut C) -> NeonResult<T>; |
| 84 | } |
| 85 | |
| 86 | /// Implement ResultExt for the std Result so it can be used any Result type |
| 87 | impl<T, E> ResultExt<T> for std::result::Result<T, E> |
| 88 | where |
| 89 | E: std::fmt::Display, |
| 90 | { |
| 91 | fn or_throw<'a, C: Context<'a>>(self, cx: &mut C) -> NeonResult<T> { |
| 92 | match self { |
| 93 | Ok(value) => Ok(value), |
| 94 | Err(error) => cx.throw_error(error.to_string()), |
| 95 | } |
| 96 | } |
| 97 | } |