Skip to content

Commit

Permalink
Moves adapter error types into its own module. (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhjort authored and dbemiller committed Jul 20, 2018
1 parent 43df402 commit 9bf9058
Show file tree
Hide file tree
Showing 23 changed files with 239 additions and 174 deletions.
32 changes: 17 additions & 15 deletions adapters/adform/adform.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"net/http"
"strings"

"net/url"
"strconv"

"github.com/buger/jsonparser"
"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/prebid/prebid-server/pbs"
"golang.org/x/net/context/ctxhttp"
"net/url"
"strconv"
)

type AdformAdapter struct {
Expand Down Expand Up @@ -134,13 +136,13 @@ func (a *AdformAdapter) Call(ctx context.Context, request *pbs.PBSRequest, bidde
responseBody := string(body)

if response.StatusCode == http.StatusBadRequest {
return nil, &adapters.BadInputError{
return nil, &errortypes.BadInput{
Message: fmt.Sprintf("HTTP status %d; body: %s", response.StatusCode, responseBody),
}
}

if response.StatusCode != 200 {
return nil, &adapters.BadServerResponseError{
return nil, &errortypes.BadServerResponse{
Message: fmt.Sprintf("HTTP status %d; body: %s", response.StatusCode, responseBody),
}
}
Expand Down Expand Up @@ -168,12 +170,12 @@ func pbsRequestToAdformRequest(a *AdformAdapter, request *pbs.PBSRequest, bidder
}
mid, err := adformAdUnit.MasterTagId.Int64()
if err != nil {
return nil, &adapters.BadInputError{
return nil, &errortypes.BadInput{
Message: err.Error(),
}
}
if mid <= 0 {
return nil, &adapters.BadInputError{
return nil, &errortypes.BadInput{
Message: fmt.Sprintf("master tag(placement) id is invalid=%s", adformAdUnit.MasterTagId),
}
}
Expand Down Expand Up @@ -341,7 +343,7 @@ func (r *adformRequest) buildAdformHeaders(a *AdformAdapter) http.Header {
func parseAdformBids(response []byte) ([]*adformBid, error) {
var bids []*adformBid
if err := json.Unmarshal(response, &bids); err != nil {
return nil, &adapters.BadServerResponseError{
return nil, &errortypes.BadServerResponse{
Message: err.Error(),
}
}
Expand Down Expand Up @@ -390,36 +392,36 @@ func openRtbToAdformRequest(request *openrtb.BidRequest) (*adformRequest, []erro
secure := false
for _, imp := range request.Imp {
if imp.Banner == nil {
errors = append(errors, &adapters.BadInputError{
errors = append(errors, &errortypes.BadInput{
Message: fmt.Sprintf("Adform adapter supports only banner Imps for now. Ignoring Imp ID=%s", imp.ID),
})
continue
}

params, _, _, err := jsonparser.Get(imp.Ext, "bidder")
if err != nil {
errors = append(errors, &adapters.BadInputError{
errors = append(errors, &errortypes.BadInput{
Message: err.Error(),
})
continue
}
var adformAdUnit adformAdUnit
if err := json.Unmarshal(params, &adformAdUnit); err != nil {
errors = append(errors, &adapters.BadInputError{
errors = append(errors, &errortypes.BadInput{
Message: err.Error(),
})
continue
}

mid, err := adformAdUnit.MasterTagId.Int64()
if err != nil {
errors = append(errors, &adapters.BadInputError{
errors = append(errors, &errortypes.BadInput{
Message: err.Error(),
})
continue
}
if mid <= 0 {
errors = append(errors, &adapters.BadInputError{
errors = append(errors, &errortypes.BadInput{
Message: fmt.Sprintf("master tag(placement) id is invalid=%s", adformAdUnit.MasterTagId),
})
continue
Expand Down Expand Up @@ -448,7 +450,7 @@ func openRtbToAdformRequest(request *openrtb.BidRequest) (*adformRequest, []erro
var extRegs openrtb_ext.ExtRegs
if request.Regs != nil {
if err := json.Unmarshal(request.Regs.Ext, &extRegs); err != nil {
errors = append(errors, &adapters.BadInputError{
errors = append(errors, &errortypes.BadInput{
Message: err.Error(),
})
}
Expand Down Expand Up @@ -527,13 +529,13 @@ func (a *AdformAdapter) MakeBids(internalRequest *openrtb.BidRequest, externalRe
}

if response.StatusCode == http.StatusBadRequest {
return nil, []error{&adapters.BadInputError{
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}

if response.StatusCode != http.StatusOK {
return nil, []error{&adapters.BadServerResponseError{
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}
Expand Down
13 changes: 7 additions & 6 deletions adapters/adtelligent/adtelligent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
)

Expand Down Expand Up @@ -92,7 +93,7 @@ func (a *AdtelligentAdapter) MakeBids(bidReq *openrtb.BidRequest, unused *adapte

var bidResp openrtb.BidResponse
if err := json.Unmarshal(httpRes.Body, &bidResp); err != nil {
return nil, []error{&adapters.BadServerResponseError{
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("error while decoding response, err: %s", err),
}}
}
Expand Down Expand Up @@ -121,7 +122,7 @@ func (a *AdtelligentAdapter) MakeBids(bidReq *openrtb.BidRequest, unused *adapte
}

if !impOK {
errors = append(errors, &adapters.BadServerResponseError{
errors = append(errors, &errortypes.BadServerResponse{
Message: fmt.Sprintf("ignoring bid id=%s, request doesn't contain any impression with id=%s", bid.ID, bid.ImpID),
})
continue
Expand All @@ -140,29 +141,29 @@ func (a *AdtelligentAdapter) MakeBids(bidReq *openrtb.BidRequest, unused *adapte
func validateImpression(imp *openrtb.Imp) (int, error) {

if imp.Banner == nil && imp.Video == nil {
return 0, &adapters.BadInputError{
return 0, &errortypes.BadInput{
Message: fmt.Sprintf("ignoring imp id=%s, Adtelligent supports only Video and Banner", imp.ID),
}
}

if 0 == len(imp.Ext) {
return 0, &adapters.BadInputError{
return 0, &errortypes.BadInput{
Message: fmt.Sprintf("ignoring imp id=%s, extImpBidder is empty", imp.ID),
}
}

var bidderExt adapters.ExtImpBidder

if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return 0, &adapters.BadInputError{
return 0, &errortypes.BadInput{
Message: fmt.Sprintf("ignoring imp id=%s, error while decoding extImpBidder, err: %s", imp.ID, err),
}
}

impExt := openrtb_ext.ExtImpAdtelligent{}
err := json.Unmarshal(bidderExt.Bidder, &impExt)
if err != nil {
return 0, &adapters.BadInputError{
return 0, &errortypes.BadInput{
Message: fmt.Sprintf("ignoring imp id=%s, error while decoding impExt, err: %s", imp.ID, err),
}
}
Expand Down
15 changes: 8 additions & 7 deletions adapters/appnexus/appnexus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
)

Expand Down Expand Up @@ -99,7 +100,7 @@ func (a *AppNexusAdapter) Call(ctx context.Context, req *pbs.PBSRequest, bidder
}

if params.PlacementId == 0 && (params.InvCode == "" || params.Member == "") {
return nil, &adapters.BadInputError{
return nil, &errortypes.BadInput{
Message: "No placement or member+invcode provided",
}
}
Expand Down Expand Up @@ -188,13 +189,13 @@ func (a *AppNexusAdapter) Call(ctx context.Context, req *pbs.PBSRequest, bidder
responseBody := string(body)

if anResp.StatusCode == http.StatusBadRequest {
return nil, &adapters.BadInputError{
return nil, &errortypes.BadInput{
Message: fmt.Sprintf("HTTP status %d; body: %s", anResp.StatusCode, responseBody),
}
}

if anResp.StatusCode != http.StatusOK {
return nil, &adapters.BadServerResponseError{
return nil, &errortypes.BadServerResponse{
Message: fmt.Sprintf("HTTP status %d; body: %s", anResp.StatusCode, responseBody),
}
}
Expand All @@ -215,7 +216,7 @@ func (a *AppNexusAdapter) Call(ctx context.Context, req *pbs.PBSRequest, bidder
for _, bid := range sb.Bid {
bidID := bidder.LookupBidID(bid.ImpID)
if bidID == "" {
return nil, &adapters.BadServerResponseError{
return nil, &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unknown ad unit code '%s'", bid.ImpID),
}
}
Expand Down Expand Up @@ -312,7 +313,7 @@ func keys(m map[string]bool) []string {
func preprocess(imp *openrtb.Imp) (string, error) {
// We don't support audio imps yet.
if imp.Audio != nil {
return "", &adapters.BadInputError{
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Appnexus doesn't support audio Imps. Ignoring Imp ID=%s", imp.ID),
}
}
Expand All @@ -339,7 +340,7 @@ func preprocess(imp *openrtb.Imp) (string, error) {
}

if appnexusExt.PlacementId == 0 && (appnexusExt.InvCode == "" || appnexusExt.Member == "") {
return "", &adapters.BadInputError{
return "", &errortypes.BadInput{
Message: "No placement or member+invcode provided",
}
}
Expand Down Expand Up @@ -403,7 +404,7 @@ func (a *AppNexusAdapter) MakeBids(internalRequest *openrtb.BidRequest, external
}

if response.StatusCode == http.StatusBadRequest {
return nil, []error{&adapters.BadInputError{
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}
Expand Down
17 changes: 9 additions & 8 deletions adapters/audienceNetwork/facebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/golang/glog"
"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/pbs"
"golang.org/x/net/context/ctxhttp"
)
Expand Down Expand Up @@ -76,7 +77,7 @@ func (a *FacebookAdapter) callOne(ctx context.Context, reqJSON bytes.Buffer) (re
result.ResponseBody = string(body)

if anResp.StatusCode == http.StatusBadRequest {
err = &adapters.BadInputError{
err = &errortypes.BadInput{
Message: fmt.Sprintf("HTTP status %d; body: %s", anResp.StatusCode, result.ResponseBody),
}
return
Expand All @@ -87,7 +88,7 @@ func (a *FacebookAdapter) callOne(ctx context.Context, reqJSON bytes.Buffer) (re
}

if anResp.StatusCode != http.StatusOK {
err = &adapters.BadServerResponseError{
err = &errortypes.BadServerResponse{
Message: fmt.Sprintf("HTTP status %d; body: %s", anResp.StatusCode, result.ResponseBody),
}
return
Expand All @@ -96,7 +97,7 @@ func (a *FacebookAdapter) callOne(ctx context.Context, reqJSON bytes.Buffer) (re
var bidResp openrtb.BidResponse
err = json.Unmarshal(body, &bidResp)
if err != nil {
err = &adapters.BadServerResponseError{
err = &errortypes.BadServerResponse{
Message: err.Error(),
}
return
Expand Down Expand Up @@ -152,7 +153,7 @@ func (a *FacebookAdapter) MakeOpenRtbBidRequest(req *pbs.PBSRequest, bidder *pbs
// if instl = 0 and type is banner, do not send non supported size
if fbReq.Imp[0].Instl == 0 && fbReq.Imp[0].Banner != nil {
if !supportedHeight[*fbReq.Imp[0].Banner.H] {
return fbReq, &adapters.BadInputError{
return fbReq, &errortypes.BadInput{
Message: "Facebook do not support banner height other than 50, 90 and 250",
}
}
Expand All @@ -163,7 +164,7 @@ func (a *FacebookAdapter) MakeOpenRtbBidRequest(req *pbs.PBSRequest, bidder *pbs
}
return fbReq, nil
} else {
return fbReq, &adapters.BadInputError{
return fbReq, &errortypes.BadInput{
Message: "No supported impressions",
}
}
Expand All @@ -179,13 +180,13 @@ func (a *FacebookAdapter) GenerateRequestsForFacebook(req *pbs.PBSRequest, bidde
return nil, err
}
if params.PlacementId == "" {
return nil, &adapters.BadInputError{
return nil, &errortypes.BadInput{
Message: "Missing placementId param",
}
}
s := strings.Split(params.PlacementId, "_")
if len(s) != 2 {
return nil, &adapters.BadInputError{
return nil, &errortypes.BadInput{
Message: fmt.Sprintf("Invalid placementId param '%s'", params.PlacementId),
}
}
Expand Down Expand Up @@ -240,7 +241,7 @@ func (a *FacebookAdapter) Call(ctx context.Context, req *pbs.PBSRequest, bidder
}
result.Bid.BidID = bidder.LookupBidID(result.Bid.AdUnitCode)
if result.Bid.BidID == "" {
result.Error = &adapters.BadServerResponseError{
result.Error = &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unknown ad unit code '%s'", result.Bid.AdUnitCode),
}
result.Bid = nil
Expand Down
7 changes: 4 additions & 3 deletions adapters/beachfront/beachfront.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
)

Expand Down Expand Up @@ -228,12 +229,12 @@ func getBannerRequest(req *openrtb.BidRequest) (BeachfrontBannerRequest, []error

for _, imp := range req.Imp {
if imp.Audio != nil {
errs = append(errs, &adapters.BadInputError{
errs = append(errs, &errortypes.BadInput{
Message: fmt.Sprintf("Beachfront doesn't support audio Imps. Ignoring Imp ID=%s", imp.ID),
})
continue
} else if imp.Native != nil {
errs = append(errs, &adapters.BadInputError{
errs = append(errs, &errortypes.BadInput{
Message: fmt.Sprintf("Beachfront doesn't support native Imps. Ignoring Imp ID=%s", imp.ID),
})
continue
Expand Down Expand Up @@ -403,7 +404,7 @@ func (a *BeachfrontAdapter) MakeBids(internalRequest *openrtb.BidRequest, extern

if len(errs) != 0 {
errors = append(errors, errs...)
err := &adapters.BadServerResponseError{
err := &errortypes.BadServerResponse{
Message: "Failed to process the beachfront response",
}

Expand Down
Loading

0 comments on commit 9bf9058

Please sign in to comment.