From 66aa22911a3753c2570eed9e29bfe114b1df6385 Mon Sep 17 00:00:00 2001 From: Evan Spendlove Date: Tue, 15 Sep 2020 20:38:29 +0000 Subject: [PATCH 1/6] Added ProbeError. --- hermes/probe/error/error.go | 70 +++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 hermes/probe/error/error.go diff --git a/hermes/probe/error/error.go b/hermes/probe/error/error.go new file mode 100644 index 0000000..6a9dc81 --- /dev/null +++ b/hermes/probe/error/error.go @@ -0,0 +1,70 @@ +// Copyright 2020 Google LLC +// +// Licensed 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 +// +// https://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. +// +// Author: Evan Spendlove, GitHub: evanSpendlove. +// +// Error implements the ProbeError used in Hermes for recording the +// exit status of functions. + +// Package error defines the ProbeError used in Hermes for recording the +// exit status of functions. +package error + +import ( + "fmt" + + "github.com/googleinterns/step224-2020/hermes/probe/metrics" +) + +// ProbeError is an error that includes an exit status for used within Hermes. +type ProbeError struct { + Status metrics.ExitStatus + Err error +} + +// Wrap returns a new ProbeError containing the error passed +// with additional context.and the status. +// Arguments: +// - status: pass the exit status associated with this error. +// - msg: pass the additional context of the error as a string. +// - innner: pass the inner error to be wrapped. +// Returns: +// - ProbeError: returns a new ProbeError object containing the args passed. +func Wrap(status metrics.ExitStatus, msg string, inner error) ProbeError { + return &ProbeError{ + Status: status, + Err: fmt.Errorf("%s: %w", msg, inner), + } +} + +// Error() returns the error string from the error. +// Returns: +// - string: returns the error as a string. +func (e *ProbeError) Error() string { + return fmt.Errorf("%v: %s: %w", e.Status, e.Msg, e.Inner) +} + +// Is returns true if the argument matches this object. +// Each error that is wrapped is examined to find a match. +// Arguments: +// - target: pass the error to be compared with. +// Returns: +// - bool: true if a match, else false. +func (e *ProbeError) Is(target error) bool { + t, ok := target.(*ProbeError) + if !ok { + return false + } + return (e.Status == t.Status) && (errors.Is(e.Err, t.Err)) +} From 04230198d601f4e55e84a3cdffd5ced4a6f34b1f Mon Sep 17 00:00:00 2001 From: Evan Spendlove Date: Wed, 16 Sep 2020 11:48:50 +0000 Subject: [PATCH 2/6] Applied code review suggestions. --- hermes/probe/error/error.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/hermes/probe/error/error.go b/hermes/probe/error/error.go index 6a9dc81..a392824 100644 --- a/hermes/probe/error/error.go +++ b/hermes/probe/error/error.go @@ -22,6 +22,7 @@ package error import ( + "errors" "fmt" "github.com/googleinterns/step224-2020/hermes/probe/metrics" @@ -29,30 +30,30 @@ import ( // ProbeError is an error that includes an exit status for used within Hermes. type ProbeError struct { + // TODO(evanSpendlove): Refactor metrics.ExitStatus to use something similar to metrics.APICallStatus. Status metrics.ExitStatus Err error } -// Wrap returns a new ProbeError containing the error passed +// New returns a new ProbeError containing the error passed // with additional context.and the status. // Arguments: // - status: pass the exit status associated with this error. -// - msg: pass the additional context of the error as a string. // - innner: pass the inner error to be wrapped. // Returns: // - ProbeError: returns a new ProbeError object containing the args passed. -func Wrap(status metrics.ExitStatus, msg string, inner error) ProbeError { +func New(status metrics.ExitStatus, inner error) *ProbeError { return &ProbeError{ Status: status, - Err: fmt.Errorf("%s: %w", msg, inner), + Err: inner, } } -// Error() returns the error string from the error. +// Error returns the error string from the error. // Returns: // - string: returns the error as a string. func (e *ProbeError) Error() string { - return fmt.Errorf("%v: %s: %w", e.Status, e.Msg, e.Inner) + return fmt.Sprintf("%v: %v", e.Status, e.Err) } // Is returns true if the argument matches this object. From 393cd9b14e6fca9ed8ac272977d2ebf4a6eebc81 Mon Sep 17 00:00:00 2001 From: Evan Spendlove Date: Wed, 16 Sep 2020 12:52:48 +0000 Subject: [PATCH 3/6] Updated comment. --- hermes/probe/error/error.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/hermes/probe/error/error.go b/hermes/probe/error/error.go index a392824..6bafc01 100644 --- a/hermes/probe/error/error.go +++ b/hermes/probe/error/error.go @@ -35,17 +35,16 @@ type ProbeError struct { Err error } -// New returns a new ProbeError containing the error passed -// with additional context.and the status. +// New returns a new ProbeError containing the error and status passed. // Arguments: // - status: pass the exit status associated with this error. -// - innner: pass the inner error to be wrapped. +// - err: pass the error to be embedded. // Returns: // - ProbeError: returns a new ProbeError object containing the args passed. -func New(status metrics.ExitStatus, inner error) *ProbeError { +func New(status metrics.ExitStatus, err error) *ProbeError { return &ProbeError{ Status: status, - Err: inner, + Err: err, } } From eded3e7d8ff233e6cbbed52e806003b2d15c0ac8 Mon Sep 17 00:00:00 2001 From: Evan Spendlove Date: Thu, 17 Sep 2020 15:29:09 +0000 Subject: [PATCH 4/6] Removed Is(). --- hermes/probe/error/error.go | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/hermes/probe/error/error.go b/hermes/probe/error/error.go index 6bafc01..b0e1a20 100644 --- a/hermes/probe/error/error.go +++ b/hermes/probe/error/error.go @@ -30,7 +30,7 @@ import ( // ProbeError is an error that includes an exit status for used within Hermes. type ProbeError struct { - // TODO(evanSpendlove): Refactor metrics.ExitStatus to use something similar to metrics.APICallStatus. + // TODO(evanSpendlove): Refactor metrics.ExitStatus to be named something similar to metrics.APICallStatus. Status metrics.ExitStatus Err error } @@ -54,17 +54,3 @@ func New(status metrics.ExitStatus, err error) *ProbeError { func (e *ProbeError) Error() string { return fmt.Sprintf("%v: %v", e.Status, e.Err) } - -// Is returns true if the argument matches this object. -// Each error that is wrapped is examined to find a match. -// Arguments: -// - target: pass the error to be compared with. -// Returns: -// - bool: true if a match, else false. -func (e *ProbeError) Is(target error) bool { - t, ok := target.(*ProbeError) - if !ok { - return false - } - return (e.Status == t.Status) && (errors.Is(e.Err, t.Err)) -} From db77f9901aa71e61875612c4e83912b37a22e5c9 Mon Sep 17 00:00:00 2001 From: Evan Spendlove Date: Fri, 18 Sep 2020 07:29:21 +0000 Subject: [PATCH 5/6] Moved error into probe.go --- hermes/probe/error/error.go | 56 ------------------------------------- hermes/probe/probe.go | 27 ++++++++++++++++++ 2 files changed, 27 insertions(+), 56 deletions(-) delete mode 100644 hermes/probe/error/error.go diff --git a/hermes/probe/error/error.go b/hermes/probe/error/error.go deleted file mode 100644 index b0e1a20..0000000 --- a/hermes/probe/error/error.go +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed 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 -// -// https://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. -// -// Author: Evan Spendlove, GitHub: evanSpendlove. -// -// Error implements the ProbeError used in Hermes for recording the -// exit status of functions. - -// Package error defines the ProbeError used in Hermes for recording the -// exit status of functions. -package error - -import ( - "errors" - "fmt" - - "github.com/googleinterns/step224-2020/hermes/probe/metrics" -) - -// ProbeError is an error that includes an exit status for used within Hermes. -type ProbeError struct { - // TODO(evanSpendlove): Refactor metrics.ExitStatus to be named something similar to metrics.APICallStatus. - Status metrics.ExitStatus - Err error -} - -// New returns a new ProbeError containing the error and status passed. -// Arguments: -// - status: pass the exit status associated with this error. -// - err: pass the error to be embedded. -// Returns: -// - ProbeError: returns a new ProbeError object containing the args passed. -func New(status metrics.ExitStatus, err error) *ProbeError { - return &ProbeError{ - Status: status, - Err: err, - } -} - -// Error returns the error string from the error. -// Returns: -// - string: returns the error as a string. -func (e *ProbeError) Error() string { - return fmt.Sprintf("%v: %v", e.Status, e.Err) -} diff --git a/hermes/probe/probe.go b/hermes/probe/probe.go index 148bd32..26f9c2c 100644 --- a/hermes/probe/probe.go +++ b/hermes/probe/probe.go @@ -38,6 +38,33 @@ import ( journalpb "github.com/googleinterns/step224-2020/hermes/proto" ) +// ProbeError is an error that includes an exit status for used within Hermes. +type ProbeError struct { + // TODO(evanSpendlove): Refactor metrics.ExitStatus to be named something similar to metrics.APICallStatus. + Status metrics.ExitStatus + Err error +} + +// NewProbeError returns a new ProbeError containing the error and status passed. +// Arguments: +// - status: pass the exit status associated with this error. +// - err: pass the error to be embedded. +// Returns: +// - ProbeError: returns a new ProbeError object containing the args passed. +func NewProbeError(status metrics.ExitStatus, err error) *ProbeError { + return &ProbeError{ + Status: status, + Err: err, + } +} + +// Error returns the error string from the error. +// Returns: +// - string: returns the error as a string. +func (e *ProbeError) Error() string { + return fmt.Sprintf("%v: %v", e.Status, e.Err) +} + // Target holds all of the required information and state for a given target run. type Target struct { // Target stores the proto config for the target to be probed. From 6cf146dcec3ce4643a2bbfe8203375f16f5d3b2c Mon Sep 17 00:00:00 2001 From: Evan Spendlove Date: Fri, 18 Sep 2020 07:30:07 +0000 Subject: [PATCH 6/6] Fixed typo in error comment. --- hermes/probe/probe.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hermes/probe/probe.go b/hermes/probe/probe.go index 26f9c2c..42a2052 100644 --- a/hermes/probe/probe.go +++ b/hermes/probe/probe.go @@ -48,7 +48,7 @@ type ProbeError struct { // NewProbeError returns a new ProbeError containing the error and status passed. // Arguments: // - status: pass the exit status associated with this error. -// - err: pass the error to be embedded. +// - err: the underlying error. // Returns: // - ProbeError: returns a new ProbeError object containing the args passed. func NewProbeError(status metrics.ExitStatus, err error) *ProbeError {