Ji-Xinyou commented on code in PR #1861: URL: https://github.com/apache/incubator-opendal/pull/1861#discussion_r1159980767
########## bindings/c/src/error.rs: ########## @@ -0,0 +1,203 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::ffi::CString; +use std::os::raw::{c_char, c_void}; + +use ::opendal as od; + +/// The wrapper type for opendal's error, wrapped because of the +/// orphan rule +pub(crate) struct OpendalError(pub(crate) od::Error); + +/// The C-compatible error type enum used in c bindings. +/// NOTICE: the type definition of [`ErrorNo`] should be aligned with +/// [`od::ErrorKind`] +#[repr(C)] +pub enum opendal_errno { + /// returning it back. For example, s3 returns an internal service error. + Unexpected, + /// Underlying service doesn't support this operation. + Unsupported, + /// The config for backend is invalid. + ConfigInvalid, + /// The given path is not found. + NotFound, + /// The given path doesn't have enough permission for this operation + PermissionDenied, + /// The given path is a directory. + IsADirectory, + /// The given path is not a directory. + NotADirectory, + /// The given path already exists thus we failed to the specified operation on it. + AlreadyExists, + /// Requests that sent to this path is over the limit, please slow down. + RateLimited, + /// The given file paths are same. + IsSameFile, + /// Unknown error, since [`opendal::ErrorKind`] is nonexhaustive + UnknownError, + /// NullPtr error, meaning that a provided pointer which need to be dereferenced is null + NullPtr, +} + +/// The C-compatible error status for opendal::Error. +/// NOTICE: the type definition of [`ErrorNo`] should be aligned with +/// [`od::ErrorStatus`] +#[repr(C)] +pub enum opendal_error_status { + /// Permanent means without external changes, the error never changes. + /// + /// For example, underlying services returns a not found error. + /// + /// Users SHOULD never retry this operation. + Permanent, + /// Temporary means this error is returned for temporary. + /// + /// For example, underlying services is rate limited or unavailable for temporary. + /// + /// Users CAN retry the operation to resolve it. + Temporary, + /// Persistent means this error used to be temporary but still failed after retry. + /// + /// For example, underlying services kept returning network errors. + /// + /// Users MAY retry this operation but it's highly possible to error again. + Persistent, +} + +impl OpendalError { + /// Convert the [`od::ErrorKind`] of [`od::Error`] to our own + /// C-compatible type + pub(crate) fn errno(&self) -> opendal_errno { + let e = &self.0; + match e.kind() { + od::ErrorKind::Unexpected => opendal_errno::Unexpected, + od::ErrorKind::Unsupported => opendal_errno::Unsupported, + od::ErrorKind::ConfigInvalid => opendal_errno::ConfigInvalid, + od::ErrorKind::NotFound => opendal_errno::NotFound, + od::ErrorKind::PermissionDenied => opendal_errno::PermissionDenied, + od::ErrorKind::IsADirectory => opendal_errno::IsADirectory, + od::ErrorKind::NotADirectory => opendal_errno::NotADirectory, + od::ErrorKind::AlreadyExists => opendal_errno::AlreadyExists, + od::ErrorKind::RateLimited => opendal_errno::RateLimited, + od::ErrorKind::IsSameFile => opendal_errno::IsSameFile, + _ => opendal_errno::UnknownError, Review Comment: If we are using pattern matching, we either panics by `unreachable!()` or add a placeholder like this, what's your opinion? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
