-
-
Notifications
You must be signed in to change notification settings - Fork 5
ref(service): Simplify semantic service errors #606
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
jan-auer
wants to merge
4
commits into
main
Choose a base branch
from
ref/semantic-service-errors
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.
+1,144
−604
Open
Changes from all commits
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| //! Sends a production-shaped service error to Sentry and waits for delivery. | ||
| //! | ||
| //! Configure this example with the same `OS__SENTRY__*` environment variables as the server. In | ||
| //! particular, `OS__SENTRY__DSN` must be set. An optional first argument selects the normal server | ||
| //! YAML configuration file. | ||
|
|
||
| use std::error::Error as _; | ||
| use std::net::TcpListener; | ||
| use std::path::Path; | ||
| use std::time::Duration; | ||
|
|
||
| use anyhow::ensure; | ||
| use objectstore_server::config::Config; | ||
| use objectstore_service::concurrency::spawn_metered; | ||
| use objectstore_service::error::{ErrorKind, ResultExt as _}; | ||
|
|
||
| const FLUSH_TIMEOUT: Duration = Duration::from_secs(10); | ||
|
|
||
| fn main() -> anyhow::Result<()> { | ||
| let config_path = std::env::args_os().nth(1); | ||
| let config = Config::load(config_path.as_deref().map(Path::new))?; | ||
|
|
||
| rustls::crypto::ring::default_provider() | ||
| .install_default() | ||
| .map_err(|_| anyhow::anyhow!("failed to install rustls crypto provider"))?; | ||
|
|
||
| // Keep the guard alive until after the explicit flush below. This is the same Sentry | ||
| // initialization used by the production server, including release, sampling, logs, tags, | ||
| // environment, and server name. | ||
| let _sentry_guard = objectstore_server::observability::init_sentry(&config) | ||
| .ok_or_else(|| anyhow::anyhow!("OS__SENTRY__DSN must be configured"))?; | ||
|
|
||
| let runtime = tokio::runtime::Builder::new_multi_thread() | ||
| .thread_name("sentry-test-rt") | ||
| .enable_all() | ||
| .worker_threads(config.runtime.worker_threads) | ||
| .build()?; | ||
| let _runtime_guard = runtime.enter(); | ||
|
|
||
| // This installs the same tracing-to-Sentry layer that reports service task failures in | ||
| // production. | ||
| objectstore_log::init(&config.logging); | ||
|
|
||
| let endpoint = unused_local_endpoint()?; | ||
| let error = runtime.block_on(async move { | ||
| let result: objectstore_service::error::Result<()> = | ||
| spawn_metered("sentry_test_backend_request", (), async move { | ||
| reqwest::Client::builder() | ||
| .no_proxy() | ||
| .timeout(Duration::from_secs(2)) | ||
| .build() | ||
| .context(ErrorKind::BackendFailure, "building the Sentry test client")? | ||
| .get(endpoint) | ||
| .send() | ||
| .await | ||
| .context(ErrorKind::BackendFailure, "sending the Sentry test request")?; | ||
| Ok(()) | ||
| }) | ||
| .await; | ||
|
|
||
| result.expect_err("request to a closed local port unexpectedly succeeded") | ||
| }); | ||
|
|
||
| ensure!(error.kind() == ErrorKind::BackendFailure); | ||
| ensure!( | ||
| error.source().is_some(), | ||
| "service error lost its reqwest source" | ||
| ); | ||
|
|
||
| let client = sentry::Hub::current() | ||
| .client() | ||
| .ok_or_else(|| anyhow::anyhow!("Sentry client was not initialized"))?; | ||
| ensure!( | ||
| client.flush(Some(FLUSH_TIMEOUT)), | ||
| "Sentry did not flush within {FLUSH_TIMEOUT:?}" | ||
| ); | ||
|
|
||
| eprintln!("Sentry service-error event submitted and flushed"); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Reserves and releases a loopback port so the HTTP request produces a real connection error. | ||
| fn unused_local_endpoint() -> anyhow::Result<String> { | ||
| let listener = TcpListener::bind(("127.0.0.1", 0))?; | ||
| let address = listener.local_addr()?; | ||
| drop(listener); | ||
| Ok(format!("http://{address}/objectstore-sentry-test")) | ||
| } |
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
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
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
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
Oops, something went wrong.
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.
API error serialization exposes preserved backend diagnostics to callers
Service failures returned by API handlers are serialized with their complete error chain, allowing authenticated callers to receive backend operation context, HTTP status, backend-provided codes/messages, and transport diagnostics such as request URLs. Restrict client responses to stable semantic ErrorKind details and retain preserved sources for logging only.
Evidence
object_getcallsservice.get_object(id, byte_range).awaitand returns non-rangeApiError::Servicefailures unchanged, while the request path supplies the object identifier used by the backend operation.ApiError::IntoResponseand batchcreate_error_partcallApiErrorResponse::from_error, which walkserror.source()and serializes every cause string into the publiccausesfield.ServiceErrorpreserves its source chain;BackendResponseErrorformats the fixed backend operation context, HTTP status, and parsed backend code/message, while transport errors retainreqwestdiagnostics.ApiError::capture()only controls logging and does not remove or redact the source chain before serialization.Identified by Warden · security-review · 6HA-968