Skip to content

Commit

Permalink
return error from resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishshinde-pubm committed Jul 16, 2024
1 parent 1d83ca7 commit 268a064
Show file tree
Hide file tree
Showing 26 changed files with 1,036 additions and 584 deletions.
32 changes: 0 additions & 32 deletions adapters/ortbbidder/errors.go

This file was deleted.

9 changes: 5 additions & 4 deletions adapters/ortbbidder/multi_request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/util"
"github.com/prebid/prebid-server/v2/util/jsonutil"
)

Expand All @@ -17,7 +18,7 @@ type multiRequestBuilder struct {
// parseRequest parse the incoming request and populates intermediate fields required for building requestData object
func (rb *multiRequestBuilder) parseRequest(request *openrtb2.BidRequest) (err error) {
if len(request.Imp) == 0 {
return errImpMissing
return util.ErrImpMissing
}

//get rawrequests without impression objects
Expand Down Expand Up @@ -56,7 +57,7 @@ func (rb *multiRequestBuilder) makeRequest() (requestData []*adapters.RequestDat
//step 1: clone request
if requestCloneRequired {
if newRequest, err = cloneRequest(rb.rawRequest); err != nil {
errs = append(errs, newBadInputError(err.Error()))
errs = append(errs, util.NewBadInputError(err.Error()))
continue
}
}
Expand All @@ -67,7 +68,7 @@ func (rb *multiRequestBuilder) makeRequest() (requestData []*adapters.RequestDat

//step 3: get endpoint
if endpoint, err = rb.getEndpoint(bidderParams); err != nil {
errs = append(errs, newBadInputError(err.Error()))
errs = append(errs, util.NewBadInputError(err.Error()))
continue
}

Expand All @@ -81,7 +82,7 @@ func (rb *multiRequestBuilder) makeRequest() (requestData []*adapters.RequestDat
}
//step 5: append new request data
if requestData, err = appendRequestData(requestData, newRequest, endpoint, []string{imp[idKey].(string)}); err != nil {
errs = append(errs, newBadInputError(err.Error()))
errs = append(errs, util.NewBadInputError(err.Error()))
}
}
return requestData, errs
Expand Down
5 changes: 3 additions & 2 deletions adapters/ortbbidder/multi_request_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/bidderparams"
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/util"
"github.com/stretchr/testify/assert"
)

Expand All @@ -35,7 +36,7 @@ func TestMultiRequestBuilderParseRequest(t *testing.T) {
},
},
want: want{
err: errImpMissing,
err: util.ErrImpMissing,
rawRequest: nil,
imps: nil,
},
Expand Down Expand Up @@ -214,7 +215,7 @@ func TestMultiRequestBuilderMakeRequest(t *testing.T) {

want: want{
requestData: nil,
errs: []error{newBadInputError("failed to replace macros in endpoint, err:template: endpointTemplate:1:2: " +
errs: []error{util.NewBadInputError("failed to replace macros in endpoint, err:template: endpointTemplate:1:2: " +
"executing \"endpointTemplate\" at <errorFunc>: error calling errorFunc: intentional error")},
},
},
Expand Down
26 changes: 13 additions & 13 deletions adapters/ortbbidder/ortbbidder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/bidderparams"
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/util"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/errortypes"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"github.com/prebid/prebid-server/v2/util/jsonutil"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server co
// MakeRequests prepares oRTB bidder-specific request information using which prebid server make call(s) to bidder.
func (o *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
if o.bidderParamsConfig == nil {
return nil, []error{newBadInputError(errNilBidderParamCfg.Error())}
return nil, []error{util.NewBadInputError(util.ErrNilBidderParamCfg.Error())}
}

requestBuilder := newRequestBuilder(
Expand All @@ -72,7 +73,7 @@ func (o *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapte
o.bidderParamsConfig.GetRequestParams(o.bidderName.String()))

if err := requestBuilder.parseRequest(request); err != nil {
return nil, []error{newBadInputError(err.Error())}
return nil, []error{util.NewBadInputError(err.Error())}
}

return requestBuilder.makeRequest()
Expand All @@ -88,25 +89,24 @@ func (o *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.R
return nil, []error{err}
}

response, err := o.makeBids(request, responseData.Body)
if err != nil {
return nil, []error{newBadServerResponseError(err.Error())}
}

return response, nil
return o.makeBids(request, responseData.Body)
}

// makeBids converts the bidderResponseBytes to a BidderResponse
// It retrieves response parameters, creates a response builder, parses the response, and builds the response.
// Finally, it converts the response builder's internal representation to an AdapterResponse and returns it.
func (o *adapter) makeBids(request *openrtb2.BidRequest, bidderResponseBytes json.RawMessage) (*adapters.BidderResponse, error) {
func (o *adapter) makeBids(request *openrtb2.BidRequest, bidderResponseBytes json.RawMessage) (*adapters.BidderResponse, []error) {
responseParmas := o.bidderParamsConfig.GetResponseParams(o.bidderName.String())
rb := newResponseBuilder(responseParmas, request)

err := rb.setPrebidBidderResponse(bidderResponseBytes)
if err != nil {
return nil, err
errs := rb.setPrebidBidderResponse(bidderResponseBytes)
if errortypes.ContainsFatalError(errs) {
return nil, errs
}

return rb.buildAdapterResponse()
bidderResponse, err := rb.buildAdapterResponse()
if err != nil {
errs = append(errs, err)
}
return bidderResponse, errs
}
5 changes: 3 additions & 2 deletions adapters/ortbbidder/ortbbidder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/bidderparams"
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/util"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/errortypes"
"github.com/prebid/prebid-server/v2/openrtb_ext"
Expand Down Expand Up @@ -148,7 +149,7 @@ func TestMakeRequests(t *testing.T) {
bidderCfg: bidderparams.NewBidderConfig(),
},
want: want{
errors: []error{newBadInputError(errImpMissing.Error())},
errors: []error{util.NewBadInputError(util.ErrImpMissing.Error())},
},
},
{
Expand All @@ -162,7 +163,7 @@ func TestMakeRequests(t *testing.T) {
bidderCfg: nil,
},
want: want{
errors: []error{newBadInputError("found nil bidderParamsConfig")},
errors: []error{util.NewBadInputError("found nil bidderParamsConfig")},
},
},
{
Expand Down
97 changes: 61 additions & 36 deletions adapters/ortbbidder/resolver/bidVideo_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,41 @@ type bidVideoResolver struct {
paramResolver
}

func (b *bidVideoResolver) retrieveFromBidderParamLocation(responseNode map[string]any, path string) (any, bool) {
func (b *bidVideoResolver) retrieveFromBidderParamLocation(responseNode map[string]any, path string) (any, error) {
value, found := util.GetValueFromLocation(responseNode, path)
if !found {
return nil, false
return nil, nil
}
return validateBidVideo(value)
video, err := validateBidVideo(value)
if err != nil {
return nil, util.NewWarning("failed to map response-param:[bidVideo] value:[%v]", value)
}
return video, nil
}

func validateBidVideo(value any) (any, bool) {
func validateBidVideo(value any) (any, error) {
bidVideoBytes, err := jsonutil.Marshal(value)
if err != nil {
return nil, false
return nil, err
}

var bidVideo openrtb_ext.ExtBidPrebidVideo
err = jsonutil.UnmarshalValid(bidVideoBytes, &bidVideo)
if err != nil {
return nil, false
return nil, err
}

var bidVideoMap map[string]any
err = jsonutil.UnmarshalValid(bidVideoBytes, &bidVideoMap)
if err != nil {
return nil, false
return nil, err
}
return bidVideoMap, true
return bidVideoMap, nil
}

func (b *bidVideoResolver) setValue(adapterBid map[string]any, value any) bool {
func (b *bidVideoResolver) setValue(adapterBid map[string]any, value any) error {
adapterBid[bidVideoKey] = value
return true
return nil
}

// bidVideoDurationResolver determines the duration of the bid based on the following hierarchy:
Expand All @@ -53,24 +57,31 @@ type bidVideoDurationResolver struct {
paramResolver
}

func (b *bidVideoDurationResolver) getFromORTBObject(bid map[string]any) (any, bool) {
dur, ok := bid[ortbFieldDuration].(float64)
if !ok || dur == 0 {
return nil, false
func (b *bidVideoDurationResolver) getFromORTBObject(bid map[string]any) (any, error) {
value, ok := bid[ortbFieldDuration]
if !ok || value == 0 {
return nil, nil
}
duration, ok := validateNumber[int64](value)
if !ok {
return nil, util.NewWarning("failed to map response-param:[bidVideoDuration] method:[standard_oRTB_param] value:[%v]", value)
}
return int64(dur), true
return duration, nil
}

func (b *bidVideoDurationResolver) retrieveFromBidderParamLocation(responseNode map[string]any, path string) (any, bool) {
func (b *bidVideoDurationResolver) retrieveFromBidderParamLocation(responseNode map[string]any, path string) (any, error) {
value, found := util.GetValueFromLocation(responseNode, path)
if !found {
return nil, false
return nil, nil
}
dur, ok := value.(float64)
return int64(dur), ok
duration, ok := validateNumber[int64](value)
if !ok {
return nil, util.NewWarning("failed to map response-param:[bidVideoDuration] method:[response_param_location] value:[%v]", value)
}
return duration, nil
}

func (b *bidVideoDurationResolver) setValue(adapterBid map[string]any, value any) bool {
func (b *bidVideoDurationResolver) setValue(adapterBid map[string]any, value any) error {
return setKeyValueInBidVideo(adapterBid, bidVideoDurationKey, value)
}

Expand All @@ -82,41 +93,55 @@ type bidVideoPrimaryCategoryResolver struct {
paramResolver
}

func (b *bidVideoPrimaryCategoryResolver) getFromORTBObject(bid map[string]any) (any, bool) {
cat, _ := bid[ortbFieldCategory].([]any)
if len(cat) == 0 {
return nil, false
func (b *bidVideoPrimaryCategoryResolver) getFromORTBObject(bid map[string]any) (any, error) {
value, found := bid[ortbFieldCategory]
if !found {
return nil, nil
}

categories, ok := value.([]any)
if !ok {
return nil, util.NewWarning("failed to map response-param:[bidVideoPrimaryCategory] method:[standard_oRTB_param] value:[%v]", value)
}

if len(categories) == 0 {
return nil, nil
}
typedCat, _ := cat[0].(string)
if len(typedCat) == 0 {
return nil, false

category, _ := categories[0].(string)
if len(category) == 0 {
return nil, util.NewWarning("failed to map response-param:[bidVideoPrimaryCategory] method:[standard_oRTB_param] value:[%v]", value)
}
return typedCat, true

return category, nil
}

func (b *bidVideoPrimaryCategoryResolver) retrieveFromBidderParamLocation(responseNode map[string]any, path string) (any, bool) {
func (b *bidVideoPrimaryCategoryResolver) retrieveFromBidderParamLocation(responseNode map[string]any, path string) (any, error) {
value, found := util.GetValueFromLocation(responseNode, path)
if !found {
return nil, false
return nil, nil
}
category, ok := value.(string)
if !ok {
return nil, util.NewWarning("failed to map response-param:[bidVideoPrimaryCategory] method:[response_param_location] value:[%v]", value)
}
cat, ok := value.(string)
return cat, ok
return category, nil
}

func (b *bidVideoPrimaryCategoryResolver) setValue(adapterBid map[string]any, value any) bool {
func (b *bidVideoPrimaryCategoryResolver) setValue(adapterBid map[string]any, value any) error {
return setKeyValueInBidVideo(adapterBid, bidVideoPrimaryCategoryKey, value)
}

func setKeyValueInBidVideo(adapterBid map[string]any, key string, value any) bool {
func setKeyValueInBidVideo(adapterBid map[string]any, key string, value any) error {
video, found := adapterBid[bidVideoKey]
if !found {
video = map[string]any{}
adapterBid[bidVideoKey] = video
}
videoTyped, ok := video.(map[string]any)
if !ok || videoTyped == nil {
return false
return util.NewWarning("failed to set key:[%s] in BidVideo, value:[%v] error:[incorrect data type]", key, value)
}
videoTyped[key] = value
return true
return nil
}
Loading

0 comments on commit 268a064

Please sign in to comment.