github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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 type v3ErrorItem struct { 20 Code int 21 Title string 22 Detail string 23 } 24 25 type v3CCError struct { 26 Errors []v3ErrorItem 27 } 28 29 const invalidTokenCode = 1000 30 31 func cloudControllerErrorHandler(statusCode int, body []byte) error { 32 response := ccErrorResponse{} 33 _ = json.Unmarshal(body, &response) 34 35 if response.Code == invalidTokenCode { 36 return errors.NewInvalidTokenError(response.Description) 37 } 38 39 var v3response v3CCError 40 _ = json.Unmarshal(body, &v3response) 41 42 if len(v3response.Errors) > 0 && v3response.Errors[0].Code == invalidTokenCode { 43 return errors.NewInvalidTokenError(v3response.Errors[0].Detail) 44 } 45 46 return errors.NewHTTPError(statusCode, strconv.Itoa(response.Code), response.Description) 47 } 48 49 func NewCloudControllerGateway(config coreconfig.Reader, clock func() time.Time, ui terminal.UI, logger trace.Printer, envDialTimeout string) Gateway { 50 return Gateway{ 51 errHandler: cloudControllerErrorHandler, 52 config: config, 53 PollingThrottle: DefaultPollingThrottle, 54 warnings: &[]string{}, 55 Clock: clock, 56 ui: ui, 57 logger: logger, 58 PollingEnabled: true, 59 DialTimeout: dialTimeout(envDialTimeout), 60 } 61 }