Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ local-build-commands.txt
*.gz
*.zip

.idea

10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Upcoming changes...
-
## [0.6.0] - 2025-09-18
### Added
- Added `name` field to component search and version response DTOs
### Changed
- Updated component DTOs to use `name` field instead of `component` field
- Upgraded `github.com/scanoss/papi` to v0.21.0
### Deprecated
- Deprecated `component` field in ComponentOutput and ComponentSearchOutput DTOs (use `name` instead)

## [0.5.0] - 2025-09-04
### Changed
Expand All @@ -19,5 +28,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- ?

[0.6.0]: https://github.com/scanoss/components/compare/v0.5.0...v0.6.0
[0.5.0]: https://github.com/scanoss/components/compare/v0.4.0...v0.5.0
[0.0.1]: https://github.com/scanoss/components/compare/v0.0.0...v0.0.1
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/lib/pq v1.10.9
github.com/scanoss/go-grpc-helper v0.9.0
github.com/scanoss/go-purl-helper v0.2.1
github.com/scanoss/papi v0.18.0
github.com/scanoss/papi v0.21.0
github.com/scanoss/zap-logging-helper v0.4.0
go.opentelemetry.io/otel v1.38.0
go.opentelemetry.io/otel/metric v1.38.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,8 @@ github.com/scanoss/go-purl-helper v0.2.1 h1:jp960a585ycyJSlqZky1NatMJBIQi/JGITDf
github.com/scanoss/go-purl-helper v0.2.1/go.mod h1:v20/bKD8G+vGrILdiq6r0hyRD2bO8frCJlu9drEcQ38=
github.com/scanoss/ipfilter/v2 v2.0.2 h1:GaB9i8kVJg9JQZm5XGStYkEpiaCVdsrj7ezI2wV/oh8=
github.com/scanoss/ipfilter/v2 v2.0.2/go.mod h1:AwrpX4XGbZ7EKISMi1d6E5csBk1nWB8+ugpvXHFcTpA=
github.com/scanoss/papi v0.18.0 h1:7t1PSDC10lfvXFNbPcFaRrffCbpy/Tq7wL2z3K4HiwY=
github.com/scanoss/papi v0.18.0/go.mod h1:Z4E/4IpwYdzHHRJXTgBCGG1GjksgrFjNW5cvhbKUfeU=
github.com/scanoss/papi v0.21.0 h1:aVt0q9pxaPHMq3QsFFnlIXju1NYpou/ziweWdIIkkPs=
github.com/scanoss/papi v0.21.0/go.mod h1:Z4E/4IpwYdzHHRJXTgBCGG1GjksgrFjNW5cvhbKUfeU=
github.com/scanoss/zap-logging-helper v0.4.0 h1:2qTYoaFa9+MlD2/1wmPtiDHfh+42NIEwgKVU3rPpl0Y=
github.com/scanoss/zap-logging-helper v0.4.0/go.mod h1:9QuEZcq73g/0Izv1tWeOWukoIK0oTBzM4jSNQ5kRR1w=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down
10 changes: 6 additions & 4 deletions pkg/dtos/component_version_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ type ComponentVersionsOutput struct {
}

type ComponentOutput struct {
Component string `json:"component"`
Purl string `json:"purl"`
Url string `json:"url"`
Versions []ComponentVersion `json:"versions"`
Name string `json:"name"`
Component string `json:"component"` // Deprecated. Component and name fields will contain the same data until
// the component field is removed
Purl string `json:"purl"`
Url string `json:"url"`
Versions []ComponentVersion `json:"versions"`
}

type ComponentVersion struct {
Expand Down
8 changes: 5 additions & 3 deletions pkg/dtos/search_component_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ type ComponentsSearchOutput struct {
}

type ComponentSearchOutput struct {
Component string `json:"component"`
Purl string `json:"purl"`
Url string `json:"url"`
Name string `json:"name"`
Component string `json:"component"` // Deprecated. Component and name fields will contain the same data until
// the component field is removed
Purl string `json:"purl"`
Url string `json:"url"`
}

func ExportComponentSearchOutput(s *zap.SugaredLogger, output ComponentsSearchOutput) ([]byte, error) {
Expand Down
128 changes: 128 additions & 0 deletions pkg/errors/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* 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",
}
}
23 changes: 8 additions & 15 deletions pkg/service/component_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ package service

import (
"context"
"errors"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/jmoiron/sqlx"
"github.com/scanoss/go-grpc-helper/pkg/grpc/database"
common "github.com/scanoss/papi/api/commonv2"
pb "github.com/scanoss/papi/api/componentsv2"
myconfig "scanoss.com/components/pkg/config"
se "scanoss.com/components/pkg/errors"
"scanoss.com/components/pkg/usecase"
"time"
)
Expand Down Expand Up @@ -54,22 +54,18 @@ func (d componentServer) SearchComponents(ctx context.Context, request *pb.CompS
s := ctxzap.Extract(ctx).Sugar()
s.Info("Processing component name request...")
if len(request.Search) == 0 && len(request.Component) == 0 && len(request.Vendor) == 0 {
statusResp := common.StatusResponse{Status: common.StatusCode_FAILED, Message: "There is no data to retrieve components"}
return &pb.CompSearchResponse{Status: &statusResp}, errors.New("there is no data to retrieve components")
return &pb.CompSearchResponse{Status: se.HandleServiceError(ctx, s, se.NewBadRequestError("No data supplied", nil))}, nil
}
dtoRequest, err := convertSearchComponentInput(s, request) // Convert to internal DTO for processing
if err != nil {
statusResp := common.StatusResponse{Status: common.StatusCode_FAILED, Message: "Problem parsing component input data"}
return &pb.CompSearchResponse{Status: &statusResp}, errors.New("problem parsing component input data")
return &pb.CompSearchResponse{Status: se.HandleServiceError(ctx, s, err)}, nil
}

// Search the KB for information about the components
compUc := usecase.NewComponents(ctx, s, d.db, database.NewDBSelectContext(s, d.db, nil, d.config.Database.Trace))
dtoComponents, err := compUc.SearchComponents(dtoRequest)
if err != nil {
s.Errorf("Failed to get components: %v", err)
statusResp := common.StatusResponse{Status: common.StatusCode_FAILED, Message: "Problems encountered extracting components data"}
return &pb.CompSearchResponse{Status: &statusResp}, nil
return &pb.CompSearchResponse{Status: se.HandleServiceError(ctx, s, err)}, nil
}
s.Debugf("Parsed Components: %+v", dtoComponents)
componentsResponse, err := convertSearchComponentOutput(s, dtoComponents) // Convert the internal data into a response object
Expand All @@ -91,23 +87,20 @@ func (d componentServer) GetComponentVersions(ctx context.Context, request *pb.C
s.Info("Processing component versions request...")
//Verify the input request
if len(request.Purl) == 0 {
statusResp := common.StatusResponse{Status: common.StatusCode_FAILED, Message: "There is no purl to retrieve component"}
return &pb.CompVersionResponse{Status: &statusResp}, errors.New("there is no purl to retrieve component")
return &pb.CompVersionResponse{Status: se.HandleServiceError(ctx, s, se.NewBadRequestError("No purl supplied", nil))}, nil
}
//Convert the request to internal DTO
dtoRequest, err := convertCompVersionsInput(s, request)
if err != nil {
statusResp := common.StatusResponse{Status: common.StatusCode_FAILED, Message: "Problem parsing component version input data"}
return &pb.CompVersionResponse{Status: &statusResp}, errors.New("problem parsing component version input data")
return &pb.CompVersionResponse{Status: se.HandleServiceError(ctx, s, err)}, nil
}
// Creates the use case
compUc := usecase.NewComponents(ctx, s, d.db, database.NewDBSelectContext(s, d.db, nil, d.config.Database.Trace))
dtoOutput, err := compUc.GetComponentVersions(dtoRequest)
if err != nil {
s.Errorf("Failed to get components: %v", err)
statusResp := common.StatusResponse{Status: common.StatusCode_FAILED, Message: "Problems encountered extracting components data"}
return &pb.CompVersionResponse{Status: &statusResp}, nil
return &pb.CompVersionResponse{Status: se.HandleServiceError(ctx, s, err)}, nil
}

reqResponse, err := convertCompVersionsOutput(s, dtoOutput)
if err != nil {
s.Errorf("Failed to convert parsed components: %v", err)
Expand Down
20 changes: 10 additions & 10 deletions pkg/service/component_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func TestComponentServer_SearchComponents(t *testing.T) {
s := NewComponentServer(db, myConfig)

var compRequestData = `{
"component": "angular",
"package": "github"
"component": "react",
"package": "npm"
}`

var compReq = pb.CompSearchRequest{}
Expand All @@ -131,13 +131,13 @@ func TestComponentServer_SearchComponents(t *testing.T) {
wantErr bool
}{
{
name: "Search for angular and purl type github without limit",
name: "Search for react and purl type npm without limit",
s: s,
args: args{
ctx: ctx,
req: &compReq,
},
want: &pb.CompSearchResponse{Status: &common.StatusResponse{Status: common.StatusCode_SUCCESS, Message: "Success"}},
want: &pb.CompSearchResponse{Status: &common.StatusResponse{Status: common.StatusCode_FAILED, Message: "No components found matching the search criteria"}},
},
{
name: "Search for a empty request",
Expand All @@ -146,8 +146,8 @@ func TestComponentServer_SearchComponents(t *testing.T) {
ctx: ctx,
req: &pb.CompSearchRequest{},
},
want: &pb.CompSearchResponse{Status: &common.StatusResponse{Status: common.StatusCode_FAILED, Message: "Problems encountered extracting components data"}},
wantErr: true,
want: &pb.CompSearchResponse{Status: &common.StatusResponse{Status: common.StatusCode_FAILED, Message: "No data supplied"}},
wantErr: false,
},
}

Expand Down Expand Up @@ -189,7 +189,7 @@ func TestComponentServer_GetComponentVersions(t *testing.T) {
s := NewComponentServer(db, myConfig)

var compVersionRequestData = `{
"purl": "pkg:npm/%40angular/elements"
"purl": "pkg:npm/react"
}`

var compVersionReq = pb.CompVersionRequest{}
Expand All @@ -210,7 +210,7 @@ func TestComponentServer_GetComponentVersions(t *testing.T) {
wantErr bool
}{
{
name: "Search for angular and purl type github without limit",
name: "Search for react and purl type npm without limit",
s: s,
args: args{
ctx: ctx,
Expand All @@ -225,8 +225,8 @@ func TestComponentServer_GetComponentVersions(t *testing.T) {
ctx: ctx,
req: &pb.CompVersionRequest{},
},
want: &pb.CompVersionResponse{Status: &common.StatusResponse{Status: common.StatusCode_FAILED, Message: "there is no purl to retrieve component"}},
wantErr: true,
want: &pb.CompVersionResponse{Status: &common.StatusResponse{Status: common.StatusCode_FAILED, Message: "No purl supplied"}},
wantErr: false,
},
}

Expand Down
13 changes: 5 additions & 8 deletions pkg/service/component_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go.opentelemetry.io/otel/metric"
"go.uber.org/zap"
"scanoss.com/components/pkg/dtos"
se "scanoss.com/components/pkg/errors"
)

// Structure for storing OTEL metrics.
Expand All @@ -28,13 +29,11 @@ func setupMetrics() {
func convertSearchComponentInput(s *zap.SugaredLogger, request *pb.CompSearchRequest) (dtos.ComponentSearchInput, error) {
data, err := json.Marshal(request)
if err != nil {
s.Errorf("Problem marshalling component request input: %v", err)
return dtos.ComponentSearchInput{}, errors.New("problem marshalling component input")
return dtos.ComponentSearchInput{}, se.NewBadRequestError("Error parsing request data", err)
}
dtoRequest, err := dtos.ParseComponentSearchInput(s, data)
if err != nil {
s.Errorf("Problem parsing component request input: %v", err)
return dtos.ComponentSearchInput{}, errors.New("problem parsing component input")
return dtos.ComponentSearchInput{}, se.NewBadRequestError("Error parsing request data", err)
}
return dtoRequest, nil
}
Expand All @@ -57,13 +56,11 @@ func convertSearchComponentOutput(s *zap.SugaredLogger, output dtos.ComponentsSe
func convertCompVersionsInput(s *zap.SugaredLogger, request *pb.CompVersionRequest) (dtos.ComponentVersionsInput, error) {
data, err := json.Marshal(request)
if err != nil {
s.Errorf("Problem marshalling component request input: %v", err)
return dtos.ComponentVersionsInput{}, errors.New("problem marshalling component version request input")
return dtos.ComponentVersionsInput{}, se.NewBadRequestError("Error parsing request data", err)
}
dtoRequest, err := dtos.ParseComponentVersionsInput(s, data)
if err != nil {
s.Errorf("Problem parsing component request input: %v", err)
return dtos.ComponentVersionsInput{}, errors.New("problem parsing component version input")
return dtos.ComponentVersionsInput{}, se.NewBadRequestError("Error parsing request data", err)
}
return dtoRequest, nil
}
Expand Down
Loading
Loading