-
Notifications
You must be signed in to change notification settings - Fork 47
Add Execution Context to Errors #480
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
Draft
stsantilena
wants to merge
6
commits into
master
Choose a base branch
from
LOG-15034-add-execution-context-to-errors
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
752ac75
Add additional error type with span trace
stsantilena cdca6a8
Update tracing subscriber layer
stsantilena 2c9fa9f
Use backtrace capture
stsantilena 596562b
Update config and makefile
stsantilena 08613b3
Update config
stsantilena 309b4dc
Merge branch 'master' into LOG-15034-add-execution-context-to-errors
stsantilena 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ extern crate notify; | |
| use futures::{stream, Stream}; | ||
| use notify::event::{CreateKind, DataChange, ModifyKind, RemoveKind, RenameMode}; | ||
| use notify::{Config, ErrorKind, EventKind, Watcher as NotifyWatcher}; | ||
| use std::backtrace::Backtrace; | ||
| use std::path::Path; | ||
| use std::rc::Rc; | ||
| use time::OffsetDateTime; | ||
|
|
@@ -83,6 +84,22 @@ pub enum Error { | |
| MaxFilesWatch, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct ContextError { | ||
| error: Error, | ||
| #[allow(dead_code)] | ||
| context: Backtrace, | ||
| } | ||
|
|
||
| impl ContextError { | ||
| pub fn new(error: Error) -> Self { | ||
| Self { | ||
| error, | ||
| context: Backtrace::capture(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum RecursiveMode { | ||
| Recursive, | ||
|
|
@@ -123,14 +140,14 @@ impl Watcher { | |
|
|
||
| /// Adds a new directory or file to watch | ||
| #[instrument(level = "trace")] | ||
| pub fn watch(&mut self, path: &Path, mode: RecursiveMode) -> Result<(), Error> { | ||
| pub fn watch(&mut self, path: &Path, mode: RecursiveMode) -> Result<(), ContextError> { | ||
| trace!("watching {:?}", path); | ||
| self.watcher.watch(path, mode.into()).map_err(|e| e.into()) | ||
| } | ||
|
|
||
| #[instrument(level = "trace")] | ||
| /// Removes a file or directory | ||
| pub fn unwatch(&mut self, path: &Path) -> Result<(), Error> { | ||
| pub fn unwatch(&mut self, path: &Path) -> Result<(), ContextError> { | ||
| trace!("unwatching {:?}", path); | ||
| self.watcher.unwatch(path).map_err(|e| e.into()) | ||
| } | ||
|
|
@@ -139,11 +156,16 @@ impl Watcher { | |
| /// | ||
| /// Returns Ok(true) when watch was found and removed. | ||
| #[instrument(level = "trace")] | ||
| pub fn unwatch_if_exists(&mut self, path: &Path) -> Result<bool, Error> { | ||
| pub fn unwatch_if_exists(&mut self, path: &Path) -> Result<bool, ContextError> { | ||
| trace!("unwatching {:?} if it exists", path); | ||
| match self.watcher.unwatch(path).map_err(|e| e.into()) { | ||
| #[allow(clippy::redundant_closure)] | ||
| match self | ||
| .watcher | ||
| .unwatch(path) | ||
| .map_err(|e| <notify::Error as Into<ContextError>>::into(e)) | ||
| { | ||
| Ok(_) => Ok(true), | ||
| Err(e) => match e { | ||
| Err(e) => match e.error { | ||
| // Ignore watch not found | ||
| Error::WatchNotFound => Ok(false), | ||
| _ => Err(e), | ||
|
|
@@ -235,15 +257,15 @@ impl Default for Watcher { | |
| } | ||
| } | ||
|
|
||
| impl From<notify::Error> for Error { | ||
| fn from(e: notify::Error) -> Error { | ||
| impl From<notify::Error> for ContextError { | ||
| fn from(e: notify::Error) -> ContextError { | ||
| match e.kind { | ||
| ErrorKind::Generic(s) => Error::Generic(s), | ||
| ErrorKind::Io(err) => Error::Io(format!("{}", err)), | ||
| ErrorKind::PathNotFound => Error::PathNotFound, | ||
| ErrorKind::WatchNotFound => Error::WatchNotFound, | ||
| ErrorKind::InvalidConfig(c) => Error::InvalidConfig(c), | ||
| ErrorKind::MaxFilesWatch => Error::MaxFilesWatch, | ||
| ErrorKind::Generic(s) => ContextError::new(Error::Generic(s)), | ||
| ErrorKind::Io(err) => ContextError::new(Error::Io(format!("{}", err))), | ||
| ErrorKind::PathNotFound => ContextError::new(Error::PathNotFound), | ||
| ErrorKind::WatchNotFound => ContextError::new(Error::WatchNotFound), | ||
| ErrorKind::InvalidConfig(c) => ContextError::new(Error::InvalidConfig(c)), | ||
| ErrorKind::MaxFilesWatch => ContextError::new(Error::MaxFilesWatch), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrapping our original |
||
| } | ||
| } | ||
| } | ||
|
|
@@ -263,6 +285,7 @@ mod tests { | |
|
|
||
| use futures::StreamExt; | ||
| use pin_utils::pin_mut; | ||
| use std::backtrace::BacktraceStatus; | ||
| use std::fs::{self, File}; | ||
| use std::io::{self, Write}; | ||
| use std::time::Duration; | ||
|
|
@@ -324,6 +347,16 @@ mod tests { | |
| }; | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_context_error_type() { | ||
| let context_error = ContextError::new(Error::Generic("test-trace".to_string())); | ||
| assert_eq!(context_error.context.status(), BacktraceStatus::Captured); | ||
| assert_eq!( | ||
| context_error.error, | ||
| Error::Generic(String::from("test-trace")) | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[cfg(unix)] | ||
| async fn test_unwatch_if_exists() { | ||
|
|
||
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.
Had to create custom error type and constructor to pass the backtrace information along.