mirror of
https://github.com/rustfs/rustfs.git
synced 2026-01-16 17:20:33 +00:00
feat:add api error message (#801)
* feat:add api error message * fix: check input * fix: test
This commit is contained in:
@@ -41,6 +41,143 @@ impl ApiError {
|
||||
source: Some(error.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn error_code_to_message(code: &S3ErrorCode) -> String {
|
||||
match code {
|
||||
S3ErrorCode::InvalidRequest => "Invalid Request".to_string(),
|
||||
S3ErrorCode::InvalidArgument => "Invalid argument".to_string(),
|
||||
S3ErrorCode::InvalidStorageClass => "Invalid storage class.".to_string(),
|
||||
S3ErrorCode::AccessDenied => "Access Denied.".to_string(),
|
||||
S3ErrorCode::BadDigest => "The Content-Md5 you specified did not match what we received.".to_string(),
|
||||
S3ErrorCode::EntityTooSmall => "Your proposed upload is smaller than the minimum allowed object size.".to_string(),
|
||||
S3ErrorCode::EntityTooLarge => "Your proposed upload exceeds the maximum allowed object size.".to_string(),
|
||||
S3ErrorCode::InternalError => "We encountered an internal error, please try again.".to_string(),
|
||||
S3ErrorCode::InvalidAccessKeyId => "The Access Key Id you provided does not exist in our records.".to_string(),
|
||||
S3ErrorCode::InvalidBucketName => "The specified bucket is not valid.".to_string(),
|
||||
S3ErrorCode::InvalidDigest => "The Content-Md5 you specified is not valid.".to_string(),
|
||||
S3ErrorCode::InvalidRange => "The requested range is not satisfiable".to_string(),
|
||||
S3ErrorCode::MalformedXML => "The XML you provided was not well-formed or did not validate against our published schema.".to_string(),
|
||||
S3ErrorCode::MissingContentLength => "You must provide the Content-Length HTTP header.".to_string(),
|
||||
S3ErrorCode::MissingSecurityHeader => "Your request was missing a required header".to_string(),
|
||||
S3ErrorCode::MissingRequestBodyError => "Request body is empty.".to_string(),
|
||||
S3ErrorCode::NoSuchBucket => "The specified bucket does not exist".to_string(),
|
||||
S3ErrorCode::NoSuchBucketPolicy => "The bucket policy does not exist".to_string(),
|
||||
S3ErrorCode::NoSuchLifecycleConfiguration => "The lifecycle configuration does not exist".to_string(),
|
||||
S3ErrorCode::NoSuchKey => "The specified key does not exist.".to_string(),
|
||||
S3ErrorCode::NoSuchUpload => "The specified multipart upload does not exist. The upload ID may be invalid, or the upload may have been aborted or completed.".to_string(),
|
||||
S3ErrorCode::NoSuchVersion => "The specified version does not exist.".to_string(),
|
||||
S3ErrorCode::NotImplemented => "A header you provided implies functionality that is not implemented".to_string(),
|
||||
S3ErrorCode::PreconditionFailed => "At least one of the pre-conditions you specified did not hold".to_string(),
|
||||
S3ErrorCode::SignatureDoesNotMatch => "The request signature we calculated does not match the signature you provided. Check your key and signing method.".to_string(),
|
||||
S3ErrorCode::MethodNotAllowed => "The specified method is not allowed against this resource.".to_string(),
|
||||
S3ErrorCode::InvalidPart => "One or more of the specified parts could not be found. The part may not have been uploaded, or the specified entity tag may not match the part's entity tag.".to_string(),
|
||||
S3ErrorCode::InvalidPartOrder => "The list of parts was not in ascending order. The parts list must be specified in order by part number.".to_string(),
|
||||
S3ErrorCode::InvalidObjectState => "The operation is not valid for the current state of the object.".to_string(),
|
||||
S3ErrorCode::AuthorizationHeaderMalformed => "The authorization header is malformed; the region is wrong; expecting 'us-east-1'.".to_string(),
|
||||
S3ErrorCode::MalformedPOSTRequest => "The body of your POST request is not well-formed multipart/form-data.".to_string(),
|
||||
S3ErrorCode::BucketNotEmpty => "The bucket you tried to delete is not empty".to_string(),
|
||||
S3ErrorCode::BucketAlreadyExists => "The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again.".to_string(),
|
||||
S3ErrorCode::BucketAlreadyOwnedByYou => "Your previous request to create the named bucket succeeded and you already own it.".to_string(),
|
||||
S3ErrorCode::AllAccessDisabled => "All access to this resource has been disabled.".to_string(),
|
||||
S3ErrorCode::InvalidPolicyDocument => "The content of the form does not meet the conditions specified in the policy document.".to_string(),
|
||||
S3ErrorCode::IncompleteBody => "You did not provide the number of bytes specified by the Content-Length HTTP header.".to_string(),
|
||||
S3ErrorCode::RequestTimeTooSkewed => "The difference between the request time and the server's time is too large.".to_string(),
|
||||
S3ErrorCode::InvalidRegion => "Region does not match.".to_string(),
|
||||
S3ErrorCode::SlowDown => "Resource requested is unreadable, please reduce your request rate".to_string(),
|
||||
S3ErrorCode::KeyTooLongError => "Your key is too long".to_string(),
|
||||
S3ErrorCode::NoSuchTagSet => "The TagSet does not exist".to_string(),
|
||||
S3ErrorCode::ObjectLockConfigurationNotFoundError => "Object Lock configuration does not exist for this bucket".to_string(),
|
||||
S3ErrorCode::InvalidBucketState => "Object Lock configuration cannot be enabled on existing buckets".to_string(),
|
||||
S3ErrorCode::NoSuchCORSConfiguration => "The CORS configuration does not exist".to_string(),
|
||||
S3ErrorCode::NoSuchWebsiteConfiguration => "The specified bucket does not have a website configuration".to_string(),
|
||||
S3ErrorCode::NoSuchObjectLockConfiguration => "The specified object does not have a ObjectLock configuration".to_string(),
|
||||
S3ErrorCode::MetadataTooLarge => "Your metadata headers exceed the maximum allowed metadata size.".to_string(),
|
||||
S3ErrorCode::ServiceUnavailable => "The service is unavailable. Please retry.".to_string(),
|
||||
S3ErrorCode::Busy => "The service is unavailable. Please retry.".to_string(),
|
||||
S3ErrorCode::EmptyRequestBody => "Request body cannot be empty.".to_string(),
|
||||
S3ErrorCode::UnauthorizedAccess => "You are not authorized to perform this operation".to_string(),
|
||||
S3ErrorCode::ExpressionTooLong => "The SQL expression is too long: The maximum byte-length for the SQL expression is 256 KB.".to_string(),
|
||||
S3ErrorCode::IllegalSqlFunctionArgument => "Illegal argument was used in the SQL function.".to_string(),
|
||||
S3ErrorCode::InvalidKeyPath => "Key path in the SQL expression is invalid.".to_string(),
|
||||
S3ErrorCode::InvalidCompressionFormat => "The file is not in a supported compression format. Only GZIP is supported at this time.".to_string(),
|
||||
S3ErrorCode::InvalidFileHeaderInfo => "The FileHeaderInfo is invalid. Only NONE, USE, and IGNORE are supported.".to_string(),
|
||||
S3ErrorCode::InvalidJsonType => "The JsonType is invalid. Only DOCUMENT and LINES are supported at this time.".to_string(),
|
||||
S3ErrorCode::InvalidQuoteFields => "The QuoteFields is invalid. Only ALWAYS and ASNEEDED are supported.".to_string(),
|
||||
S3ErrorCode::InvalidRequestParameter => "The value of a parameter in SelectRequest element is invalid. Check the service API documentation and try again.".to_string(),
|
||||
S3ErrorCode::InvalidDataSource => "Invalid data source type. Only CSV and JSON are supported at this time.".to_string(),
|
||||
S3ErrorCode::InvalidExpressionType => "The ExpressionType is invalid. Only SQL expressions are supported at this time.".to_string(),
|
||||
S3ErrorCode::InvalidDataType => "The SQL expression contains an invalid data type.".to_string(),
|
||||
S3ErrorCode::InvalidTextEncoding => "Invalid encoding type. Only UTF-8 encoding is supported at this time.".to_string(),
|
||||
S3ErrorCode::InvalidTableAlias => "The SQL expression contains an invalid table alias.".to_string(),
|
||||
S3ErrorCode::MissingRequiredParameter => "The SelectRequest entity is missing a required parameter. Check the service documentation and try again.".to_string(),
|
||||
S3ErrorCode::ObjectSerializationConflict => "The SelectRequest entity can only contain one of CSV or JSON. Check the service documentation and try again.".to_string(),
|
||||
S3ErrorCode::UnsupportedSqlOperation => "Encountered an unsupported SQL operation.".to_string(),
|
||||
S3ErrorCode::UnsupportedSqlStructure => "Encountered an unsupported SQL structure. Check the SQL Reference.".to_string(),
|
||||
S3ErrorCode::UnsupportedSyntax => "Encountered invalid syntax.".to_string(),
|
||||
S3ErrorCode::UnsupportedRangeHeader => "Range header is not supported for this operation.".to_string(),
|
||||
S3ErrorCode::LexerInvalidChar => "The SQL expression contains an invalid character.".to_string(),
|
||||
S3ErrorCode::LexerInvalidOperator => "The SQL expression contains an invalid literal.".to_string(),
|
||||
S3ErrorCode::LexerInvalidLiteral => "The SQL expression contains an invalid operator.".to_string(),
|
||||
S3ErrorCode::LexerInvalidIONLiteral => "The SQL expression contains an invalid operator.".to_string(),
|
||||
S3ErrorCode::ParseExpectedDatePart => "Did not find the expected date part in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedKeyword => "Did not find the expected keyword in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedTokenType => "Did not find the expected token in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpected2TokenTypes => "Did not find the expected token in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedNumber => "Did not find the expected number in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedRightParenBuiltinFunctionCall => "Did not find the expected right parenthesis character in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedTypeName => "Did not find the expected type name in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedWhenClause => "Did not find the expected WHEN clause in the SQL expression. CASE is not supported.".to_string(),
|
||||
S3ErrorCode::ParseUnsupportedToken => "The SQL expression contains an unsupported token.".to_string(),
|
||||
S3ErrorCode::ParseUnsupportedLiteralsGroupBy => "The SQL expression contains an unsupported use of GROUP BY.".to_string(),
|
||||
S3ErrorCode::ParseExpectedMember => "The SQL expression contains an unsupported use of MEMBER.".to_string(),
|
||||
S3ErrorCode::ParseUnsupportedSelect => "The SQL expression contains an unsupported use of SELECT.".to_string(),
|
||||
S3ErrorCode::ParseUnsupportedCase => "The SQL expression contains an unsupported use of CASE.".to_string(),
|
||||
S3ErrorCode::ParseUnsupportedCaseClause => "The SQL expression contains an unsupported use of CASE.".to_string(),
|
||||
S3ErrorCode::ParseUnsupportedAlias => "The SQL expression contains an unsupported use of ALIAS.".to_string(),
|
||||
S3ErrorCode::ParseUnsupportedSyntax => "The SQL expression contains unsupported syntax.".to_string(),
|
||||
S3ErrorCode::ParseUnknownOperator => "The SQL expression contains an invalid operator.".to_string(),
|
||||
S3ErrorCode::ParseMissingIdentAfterAt => "Did not find the expected identifier after the @ symbol in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseUnexpectedOperator => "The SQL expression contains an unexpected operator.".to_string(),
|
||||
S3ErrorCode::ParseUnexpectedTerm => "The SQL expression contains an unexpected term.".to_string(),
|
||||
S3ErrorCode::ParseUnexpectedToken => "The SQL expression contains an unexpected token.".to_string(),
|
||||
S3ErrorCode::ParseExpectedExpression => "Did not find the expected SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedLeftParenAfterCast => "Did not find expected the left parenthesis in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedLeftParenValueConstructor => "Did not find expected the left parenthesis in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedLeftParenBuiltinFunctionCall => "Did not find the expected left parenthesis in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedArgumentDelimiter => "Did not find the expected argument delimiter in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseCastArity => "The SQL expression CAST has incorrect arity.".to_string(),
|
||||
S3ErrorCode::ParseInvalidTypeParam => "The SQL expression contains an invalid parameter value.".to_string(),
|
||||
S3ErrorCode::ParseEmptySelect => "The SQL expression contains an empty SELECT.".to_string(),
|
||||
S3ErrorCode::ParseSelectMissingFrom => "GROUP is not supported in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedIdentForGroupName => "GROUP is not supported in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedIdentForAlias => "Did not find the expected identifier for the alias in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseUnsupportedCallWithStar => "Only COUNT with (*) as a parameter is supported in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseNonUnaryAgregateFunctionCall => "Only one argument is supported for aggregate functions in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseMalformedJoin => "JOIN is not supported in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseExpectedIdentForAt => "Did not find the expected identifier for AT name in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseAsteriskIsNotAloneInSelectList => "Other expressions are not allowed in the SELECT list when '*' is used without dot notation in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseCannotMixSqbAndWildcardInSelectList => "Cannot mix [] and * in the same expression in a SELECT list in SQL expression.".to_string(),
|
||||
S3ErrorCode::ParseInvalidContextForWildcardInSelectList => "Invalid use of * in SELECT list in the SQL expression.".to_string(),
|
||||
S3ErrorCode::IncorrectSqlFunctionArgumentType => "Incorrect type of arguments in function call in the SQL expression.".to_string(),
|
||||
S3ErrorCode::ValueParseFailure => "Time stamp parse failure in the SQL expression.".to_string(),
|
||||
S3ErrorCode::EvaluatorInvalidArguments => "Incorrect number of arguments in the function call in the SQL expression.".to_string(),
|
||||
S3ErrorCode::IntegerOverflow => "Int overflow or underflow in the SQL expression.".to_string(),
|
||||
S3ErrorCode::LikeInvalidInputs => "Invalid argument given to the LIKE clause in the SQL expression.".to_string(),
|
||||
S3ErrorCode::CastFailed => "Attempt to convert from one data type to another using CAST failed in the SQL expression.".to_string(),
|
||||
S3ErrorCode::InvalidCast => "Attempt to convert from one data type to another using CAST failed in the SQL expression.".to_string(),
|
||||
S3ErrorCode::EvaluatorInvalidTimestampFormatPattern => "Time stamp format pattern requires additional fields in the SQL expression.".to_string(),
|
||||
S3ErrorCode::EvaluatorInvalidTimestampFormatPatternSymbolForParsing => "Time stamp format pattern contains a valid format symbol that cannot be applied to time stamp parsing in the SQL expression.".to_string(),
|
||||
S3ErrorCode::EvaluatorTimestampFormatPatternDuplicateFields => "Time stamp format pattern contains multiple format specifiers representing the time stamp field in the SQL expression.".to_string(),
|
||||
S3ErrorCode::EvaluatorTimestampFormatPatternHourClockAmPmMismatch => "Time stamp format pattern contains unterminated token in the SQL expression.".to_string(),
|
||||
S3ErrorCode::EvaluatorUnterminatedTimestampFormatPatternToken => "Time stamp format pattern contains an invalid token in the SQL expression.".to_string(),
|
||||
S3ErrorCode::EvaluatorInvalidTimestampFormatPatternToken => "Time stamp format pattern contains an invalid token in the SQL expression.".to_string(),
|
||||
S3ErrorCode::EvaluatorInvalidTimestampFormatPatternSymbol => "Time stamp format pattern contains an invalid symbol in the SQL expression.".to_string(),
|
||||
S3ErrorCode::EvaluatorBindingDoesNotExist => "A column name or a path provided does not exist in the SQL expression".to_string(),
|
||||
S3ErrorCode::InvalidColumnIndex => "The column index is invalid. Please check the service documentation and try again.".to_string(),
|
||||
S3ErrorCode::UnsupportedFunction => "Encountered an unsupported SQL function.".to_string(),
|
||||
_ => code.as_str().to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ApiError> for S3Error {
|
||||
@@ -87,9 +224,14 @@ impl From<StorageError> for ApiError {
|
||||
_ => S3ErrorCode::InternalError,
|
||||
};
|
||||
|
||||
let message = if code == S3ErrorCode::InternalError {
|
||||
err.to_string()
|
||||
} else {
|
||||
ApiError::error_code_to_message(&code)
|
||||
};
|
||||
ApiError {
|
||||
code,
|
||||
message: err.to_string(),
|
||||
message,
|
||||
source: Some(Box::new(err)),
|
||||
}
|
||||
}
|
||||
@@ -186,7 +328,6 @@ mod tests {
|
||||
let api_error: ApiError = storage_error.into();
|
||||
|
||||
assert_eq!(api_error.code, S3ErrorCode::NoSuchBucket);
|
||||
assert!(api_error.message.contains("test-bucket"));
|
||||
assert!(api_error.source.is_some());
|
||||
|
||||
// Test that source can be downcast back to StorageError
|
||||
|
||||
@@ -2165,6 +2165,9 @@ impl S3 for FS {
|
||||
|
||||
let prefix = prefix.unwrap_or_default();
|
||||
let max_keys = max_keys.unwrap_or(1000);
|
||||
if max_keys < 0 {
|
||||
return Err(S3Error::with_message(S3ErrorCode::InvalidArgument, "Invalid max keys".to_string()));
|
||||
}
|
||||
|
||||
let delimiter = delimiter.filter(|v| !v.is_empty());
|
||||
let start_after = start_after.filter(|v| !v.is_empty());
|
||||
@@ -2392,6 +2395,10 @@ impl S3 for FS {
|
||||
}
|
||||
};
|
||||
|
||||
if size == -1 {
|
||||
return Err(s3_error!(UnexpectedContent));
|
||||
}
|
||||
|
||||
let body = StreamReader::new(body.map(|f| f.map_err(|e| std::io::Error::other(e.to_string()))));
|
||||
|
||||
// let body = Box::new(StreamReader::new(body.map(|f| f.map_err(|e| std::io::Error::other(e.to_string())))));
|
||||
@@ -3659,11 +3666,6 @@ impl S3 for FS {
|
||||
return Err(S3Error::with_message(S3ErrorCode::InternalError, "Not init".to_string()));
|
||||
};
|
||||
|
||||
// Validate tag_set length doesn't exceed 10
|
||||
if tagging.tag_set.len() > 10 {
|
||||
return Err(s3_error!(InvalidArgument, "Object tags cannot be greater than 10"));
|
||||
}
|
||||
|
||||
let mut tag_keys = std::collections::HashSet::with_capacity(tagging.tag_set.len());
|
||||
for tag in &tagging.tag_set {
|
||||
let key = tag
|
||||
|
||||
Reference in New Issue
Block a user