github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/net/cloud_controller_gateway.go (about)

     1  package net
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/terminal"
    11  	"code.cloudfoundry.org/cli/cf/trace"
    12  )
    13  
    14  type ccErrorResponse struct {
    15  	Code        int
    16  	Description string
    17  }
    18  
    19  const invalidTokenCode = 1000
    20  
    21  func cloudControllerErrorHandler(statusCode int, body []byte) error {
    22  	response := ccErrorResponse{}
    23  	_ = json.Unmarshal(body, &response)
    24  
    25  	if response.Code == invalidTokenCode {
    26  		return errors.NewInvalidTokenError(response.Description)
    27  	}
    28  
    29  	return errors.NewHTTPError(statusCode, strconv.Itoa(response.Code), response.Description)
    30  }
    31  
    32  func NewCloudControllerGateway(config coreconfig.Reader, clock func() time.Time, ui terminal.UI, logger trace.Printer, envDialTimeout string) Gateway {
    33  	return Gateway{
    34  		errHandler:      cloudControllerErrorHandler,
    35  		config:          config,
    36  		PollingThrottle: DefaultPollingThrottle,
    37  		warnings:        &[]string{},
    38  		Clock:           clock,
    39  		ui:              ui,
    40  		logger:          logger,
    41  		PollingEnabled:  true,
    42  		DialTimeout:     dialTimeout(envDialTimeout),
    43  	}
    44  }