-
Notifications
You must be signed in to change notification settings - Fork 113
feat: add centralized LakeFS error handling for multipart upload and dataset version operations #4177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xuang7
wants to merge
3
commits into
apache:main
Choose a base branch
from
xuang7:feat/handle-lakefs-exception
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: add centralized LakeFS error handling for multipart upload and dataset version operations #4177
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
file-service/src/main/scala/org/apache/texera/service/util/LakeFSExceptionHandler.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.apache.texera.service.util | ||
|
|
||
| import jakarta.ws.rs._ | ||
| import jakarta.ws.rs.core.{MediaType, Response} | ||
| import org.slf4j.LoggerFactory | ||
|
|
||
| import scala.jdk.CollectionConverters._ | ||
|
|
||
| object LakeFSExceptionHandler { | ||
| private val logger = LoggerFactory.getLogger(getClass) | ||
|
|
||
| private val fallbackMessages = Map( | ||
| 400 -> "LakeFS rejected the request. Please verify the parameters (repository/branch/path) and try again.", | ||
| 401 -> "Authentication with LakeFS failed.", | ||
| 403 -> "Permission denied by LakeFS.", | ||
| 404 -> "LakeFS resource not found. The repository/branch/object may not exist.", | ||
| 409 -> "LakeFS reported a conflict. Another operation may be in progress.", | ||
| 420 -> "Too many requests to LakeFS." | ||
| ).withDefaultValue( | ||
| "LakeFS request failed due to an unexpected server error." | ||
| ) | ||
|
|
||
| /** | ||
| * Wraps a LakeFS call with centralized error handling. | ||
| */ | ||
| def withLakeFSErrorHandling[T](call: => T): T = { | ||
| try { | ||
| call | ||
| } catch { | ||
| case e: io.lakefs.clients.sdk.ApiException => handleException(e) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts LakeFS ApiException to appropriate HTTP exception | ||
| */ | ||
| private def handleException(e: io.lakefs.clients.sdk.ApiException): Nothing = { | ||
| val code = e.getCode | ||
| val rawBody = Option(e.getResponseBody).filter(_.nonEmpty) | ||
| val message = s"${fallbackMessages(code)}" | ||
|
|
||
| logger.warn(s"LakeFS error $code, ${e.getMessage}, body: ${rawBody.getOrElse("N/A")}") | ||
|
|
||
| def errorResponse(status: Int): Response = | ||
| Response | ||
| .status(status) | ||
| .entity(Map("message" -> message).asJava) | ||
| .`type`(MediaType.APPLICATION_JSON) | ||
| .build() | ||
|
|
||
| throw (code match { | ||
| case 400 => new BadRequestException(errorResponse(400)) | ||
| case 401 => new NotAuthorizedException(errorResponse(401)) | ||
| case 403 => new ForbiddenException(errorResponse(403)) | ||
| case 404 => new NotFoundException(errorResponse(404)) | ||
| case c if c >= 400 && c < 500 => new WebApplicationException(errorResponse(c)) | ||
| case _ => new InternalServerErrorException(errorResponse(500)) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xuang7 There is another approach with scala implicit, it will look like the next code snippet, in your preference, is this cleaner?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. I currently centralized the LakeFS exception→HTTP mapping in the file-service (LakeFSExceptionHandler). The safeCall approach also looks clean and would centralize handling at the storage layer near LakeFSStorageClient. Would you recommend switching to LakeFSStorageClient.safeCall(...) so all callers share the same handler?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can keep this approach for now.
It would be good to open a discussion, and would be good if @Ma77Ball can give his input since he is working in logging. Here is the things to discuss later:
I think this PR is really about how we want error handling to work across the services calls (LakeFS now, others later): do we force callers to deal with failures, or do we centralize + make it easy/consistent.
Option 1: change defs and force handling (typed)
Return
Either[StorageError, A](orF[Either[...]]). Caller can’t ignore it.Pros: type-safe + enforced. Cons: signature churn + lots of callsite updates + need
Errormapping.Option 2: implicit +
Dynamic.safewrapper (ergonomic, but risky)This option adds automatic logs
Nice callsite but reflection + weak typing (
Any) + possible runtime method lookup issues. Notice this method does not require any changes to the def like commit, or uploadPart but it is optional to the caller to use.safeandChanging the return type (2.1)
Keeping the return type and throwing (2.2)