Elffuss BraidHub

kikocis/lancedb public

main · 7.9 KiBRawPermalink
1// Copyright 2024 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
15use std::str::Utf8Error;
16
17use arrow_schema::ArrowError;
18use jni::errors::Error as JniError;
19use serde_json::Error as JsonError;
20use snafu::{Location, Snafu};
21
22type BoxedError = Box<dyn std::error::Error + Send + Sync + 'static>;
23
24/// Java Exception types
25pub enum JavaException {
26 IllegalArgumentException,
27 IOException,
28 RuntimeException,
29}
30
31impl JavaException {
32 pub fn as_str(&self) -> &str {
33 match self {
34 Self::IllegalArgumentException => "java/lang/IllegalArgumentException",
35 Self::IOException => "java/io/IOException",
36 Self::RuntimeException => "java/lang/RuntimeException",
37 }
38 }
39}
40/// TODO(lu) change to lancedb-jni
41#[derive(Debug, Snafu)]
42#[snafu(visibility(pub))]
43pub enum Error {
44 #[snafu(display("JNI error: {message}, {location}"))]
45 Jni { message: String, location: Location },
46 #[snafu(display("Invalid argument: {message}, {location}"))]
47 InvalidArgument { message: String, location: Location },
48 #[snafu(display("IO error: {source}, {location}"))]
49 IO {
50 source: BoxedError,
51 location: Location,
52 },
53 #[snafu(display("Arrow error: {message}, {location}"))]
54 Arrow { message: String, location: Location },
55 #[snafu(display("Index error: {message}, {location}"))]
56 Index { message: String, location: Location },
57 #[snafu(display("JSON error: {message}, {location}"))]
58 JSON { message: String, location: Location },
59 #[snafu(display("Dataset at path {path} was not found, {location}"))]
60 DatasetNotFound { path: String, location: Location },
61 #[snafu(display("Dataset already exists: {uri}, {location}"))]
62 DatasetAlreadyExists { uri: String, location: Location },
63 #[snafu(display("Table '{name}' already exists"))]
64 TableAlreadyExists { name: String },
65 #[snafu(display("Table '{name}' was not found"))]
66 TableNotFound { name: String },
67 #[snafu(display("Invalid table name '{name}': {reason}"))]
68 InvalidTableName { name: String, reason: String },
69 #[snafu(display("Embedding function '{name}' was not found: {reason}, {location}"))]
70 EmbeddingFunctionNotFound {
71 name: String,
72 reason: String,
73 location: Location,
74 },
75 #[snafu(display("Other Lance error: {message}, {location}"))]
76 OtherLance { message: String, location: Location },
77 #[snafu(display("Other LanceDB error: {message}, {location}"))]
78 OtherLanceDB { message: String, location: Location },
79}
80
81impl Error {
82 /// Throw as Java Exception
83 pub fn throw(&self, env: &mut jni::JNIEnv) {
84 match self {
85 Self::InvalidArgument { .. }
86 | Self::DatasetNotFound { .. }
87 | Self::DatasetAlreadyExists { .. }
88 | Self::TableAlreadyExists { .. }
89 | Self::TableNotFound { .. }
90 | Self::InvalidTableName { .. }
91 | Self::EmbeddingFunctionNotFound { .. } => {
92 self.throw_as(env, JavaException::IllegalArgumentException)
93 }
94 Self::IO { .. } | Self::Index { .. } => self.throw_as(env, JavaException::IOException),
95 Self::Arrow { .. }
96 | Self::JSON { .. }
97 | Self::OtherLance { .. }
98 | Self::OtherLanceDB { .. }
99 | Self::Jni { .. } => self.throw_as(env, JavaException::RuntimeException),
100 }
101 }
102
103 /// Throw as an concrete Java Exception
104 pub fn throw_as(&self, env: &mut jni::JNIEnv, exception: JavaException) {
105 let message = &format!(
106 "Error when throwing Java exception: {}:{}",
107 exception.as_str(),
108 self
109 );
110 env.throw_new(exception.as_str(), self.to_string())
111 .expect(message);
112 }
113}
114
115pub type Result<T> = std::result::Result<T, Error>;
116
117trait ToSnafuLocation {
118 fn to_snafu_location(&'static self) -> snafu::Location;
119}
120
121impl ToSnafuLocation for std::panic::Location<'static> {
122 fn to_snafu_location(&'static self) -> snafu::Location {
123 snafu::Location::new(self.file(), self.line(), self.column())
124 }
125}
126
127impl From<JniError> for Error {
128 #[track_caller]
129 fn from(source: JniError) -> Self {
130 Self::Jni {
131 message: source.to_string(),
132 location: std::panic::Location::caller().to_snafu_location(),
133 }
134 }
135}
136
137impl From<Utf8Error> for Error {
138 #[track_caller]
139 fn from(source: Utf8Error) -> Self {
140 Self::InvalidArgument {
141 message: source.to_string(),
142 location: std::panic::Location::caller().to_snafu_location(),
143 }
144 }
145}
146
147impl From<ArrowError> for Error {
148 #[track_caller]
149 fn from(source: ArrowError) -> Self {
150 Self::Arrow {
151 message: source.to_string(),
152 location: std::panic::Location::caller().to_snafu_location(),
153 }
154 }
155}
156
157impl From<JsonError> for Error {
158 #[track_caller]
159 fn from(source: JsonError) -> Self {
160 Self::JSON {
161 message: source.to_string(),
162 location: std::panic::Location::caller().to_snafu_location(),
163 }
164 }
165}
166
167impl From<lance::Error> for Error {
168 #[track_caller]
169 fn from(source: lance::Error) -> Self {
170 match source {
171 lance::Error::DatasetNotFound {
172 path,
173 source: _,
174 location,
175 } => Self::DatasetNotFound { path, location },
176 lance::Error::DatasetAlreadyExists { uri, location } => {
177 Self::DatasetAlreadyExists { uri, location }
178 }
179 lance::Error::IO { source, location } => Self::IO { source, location },
180 lance::Error::Arrow { message, location } => Self::Arrow { message, location },
181 lance::Error::Index { message, location } => Self::Index { message, location },
182 lance::Error::InvalidInput { source, location } => Self::InvalidArgument {
183 message: source.to_string(),
184 location,
185 },
186 _ => Self::OtherLance {
187 message: source.to_string(),
188 location: std::panic::Location::caller().to_snafu_location(),
189 },
190 }
191 }
192}
193
194impl From<lancedb::Error> for Error {
195 #[track_caller]
196 fn from(source: lancedb::Error) -> Self {
197 match source {
198 lancedb::Error::InvalidTableName { name, reason } => {
199 Self::InvalidTableName { name, reason }
200 }
201 lancedb::Error::InvalidInput { message } => Self::InvalidArgument {
202 message,
203 location: std::panic::Location::caller().to_snafu_location(),
204 },
205 lancedb::Error::TableNotFound { name } => Self::TableNotFound { name },
206 lancedb::Error::TableAlreadyExists { name } => Self::TableAlreadyExists { name },
207 lancedb::Error::EmbeddingFunctionNotFound { name, reason } => {
208 Self::EmbeddingFunctionNotFound {
209 name,
210 reason,
211 location: std::panic::Location::caller().to_snafu_location(),
212 }
213 }
214 lancedb::Error::Arrow { source } => Self::Arrow {
215 message: source.to_string(),
216 location: std::panic::Location::caller().to_snafu_location(),
217 },
218 lancedb::Error::Lance { source } => Self::from(source),
219 _ => Self::OtherLanceDB {
220 message: source.to_string(),
221 location: std::panic::Location::caller().to_snafu_location(),
222 },
223 }
224 }
225}