github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/net/cloud_controller_gateway.go (about)

     1  package net
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     9  	"github.com/cloudfoundry/cli/cf/errors"
    10  	"github.com/cloudfoundry/cli/cf/terminal"
    11  )
    12  
    13  type ccErrorResponse struct {
    14  	Code        int
    15  	Description string
    16  }
    17  
    18  func cloudControllerErrorHandler(statusCode int, body []byte) error {
    19  	response := ccErrorResponse{}
    20  	json.Unmarshal(body, &response)
    21  
    22  	if response.Code == 1000 { // MAGICKAL NUMBERS AHOY
    23  		return errors.NewInvalidTokenError(response.Description)
    24  	} else {
    25  		return errors.NewHttpError(statusCode, strconv.Itoa(response.Code), response.Description)
    26  	}
    27  }
    28  
    29  func NewCloudControllerGateway(config core_config.Reader, clock func() time.Time, ui terminal.UI) Gateway {
    30  	gateway := newGateway(cloudControllerErrorHandler, config, ui)
    31  	gateway.Clock = clock
    32  	gateway.PollingEnabled = true
    33  	return gateway
    34  }