-
Notifications
You must be signed in to change notification settings - Fork 0
Chore/refactor protobuf definition #34
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4f45ca6
chore:SP-3278 Implement new papi protobuf definition
agustingroh a05a73d
chore:SP-SP-3279 implements service error handler
agustingroh e883eca
chore: Upgrades scanoss/papi to v0.21.0
agustingroh d373977
chore: Removed unsed methods on error.go file and improve documentati…
agustingroh 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 |
|---|---|---|
|
|
@@ -34,3 +34,5 @@ local-build-commands.txt | |
| *.gz | ||
| *.zip | ||
|
|
||
| .idea | ||
|
|
||
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
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 |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
agustingroh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /* | ||
| * Copyright (C) 2018-2022 SCANOSS.COM | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 2 of the License, or | ||
| * (at your option) any later version. | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package errors | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| common "github.com/scanoss/papi/api/commonv2" | ||
| "go.uber.org/zap" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/metadata" | ||
| ) | ||
|
|
||
| // ServiceError represents a service-level error with HTTP status mapping and additional context.. | ||
| type ServiceError struct { | ||
| Message string // Human-readable error message | ||
| HTTPCode int // HTTP status code to return to client | ||
| InternalCode string // Internal error code for logging/monitoring | ||
| Err error // Wrapped original error for error chain | ||
| Details map[string]interface{} // Optional additional context | ||
| } | ||
|
|
||
| // Error implements the error interface. | ||
| func (e *ServiceError) Error() string { | ||
| if e.Err != nil { | ||
| return fmt.Sprintf("%s: %v", e.Message, e.Err) | ||
| } | ||
| return e.Message | ||
| } | ||
|
|
||
| // GetHTTPCode returns the HTTP status code for this error with fallback to 500. | ||
| func (e *ServiceError) GetHTTPCode() int { | ||
| if e.HTTPCode == 0 { | ||
| return http.StatusInternalServerError | ||
| } | ||
| return e.HTTPCode | ||
| } | ||
|
|
||
| // NewBadRequestError Use for: missing required fields, malformed input, invalid parameters. | ||
| func NewBadRequestError(message string, err error) *ServiceError { | ||
| return &ServiceError{ | ||
| Message: message, | ||
| HTTPCode: http.StatusBadRequest, | ||
| InternalCode: "BAD_REQUEST", | ||
| Err: err, | ||
| } | ||
| } | ||
|
|
||
| // NewNotFoundError Use for: ecosystem not found, dependencies not found, resource missing. | ||
| func NewNotFoundError(resource string) *ServiceError { | ||
| return &ServiceError{ | ||
| Message: resource, | ||
| HTTPCode: http.StatusNotFound, | ||
| InternalCode: "NOT_FOUND", | ||
| Err: nil, | ||
| } | ||
| } | ||
|
|
||
| // IsServiceError checks if an error is a ServiceError. | ||
| func IsServiceError(err error) bool { | ||
| var serviceErr *ServiceError | ||
| return errors.As(err, &serviceErr) | ||
| } | ||
|
|
||
| // GetServiceError extracts a ServiceError from an error chain. | ||
| func GetServiceError(err error) (*ServiceError, bool) { | ||
| var serviceErr *ServiceError | ||
| if errors.As(err, &serviceErr) { | ||
| return serviceErr, true | ||
| } | ||
| return nil, false | ||
| } | ||
|
|
||
| // HandleServiceError converts a ServiceError to a gRPC response with proper HTTP status. | ||
| func HandleServiceError(ctx context.Context, s *zap.SugaredLogger, err error) *common.StatusResponse { | ||
| var serviceErr *ServiceError | ||
| if IsServiceError(err) { | ||
| serviceErr, _ = GetServiceError(err) | ||
| // Set HTTP trailer based on custom error | ||
| trailerErr := grpc.SetTrailer(ctx, metadata.Pairs("x-http-code", fmt.Sprintf("%d", serviceErr.GetHTTPCode()))) | ||
| if trailerErr != nil { | ||
| s.Debugf("error setting x-http-code to trailer: %v", trailerErr) | ||
| } | ||
|
|
||
| // Log with structured data for monitoring | ||
| s.Errorw("service error", | ||
| "error", serviceErr.Error(), | ||
| "http_code", serviceErr.GetHTTPCode(), | ||
| "internal_code", serviceErr.InternalCode, | ||
| "details", serviceErr.Details, | ||
| ) | ||
|
|
||
| return &common.StatusResponse{ | ||
| Status: common.StatusCode_FAILED, | ||
| Message: serviceErr.Message, | ||
| } | ||
| } | ||
|
|
||
| // Default to 500 for unknown errors | ||
| trailerErr := grpc.SetTrailer(ctx, metadata.Pairs("x-http-code", "500")) | ||
| if trailerErr != nil { | ||
| s.Debugf("error setting x-http-code to trailer: %v", trailerErr) | ||
| } | ||
|
|
||
| s.Errorw("unhandled error", "error", err.Error()) | ||
|
|
||
| return &common.StatusResponse{ | ||
| Status: common.StatusCode_FAILED, | ||
| Message: "internal server error", | ||
| } | ||
| } | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.