当前位置: 首页>>代码示例>>Golang>>正文


Golang errors.NewUnauthorizedError函数代码示例

本文整理汇总了Golang中github.com/cloudfoundry/noaa/errors.NewUnauthorizedError函数的典型用法代码示例。如果您正苦于以下问题:Golang NewUnauthorizedError函数的具体用法?Golang NewUnauthorizedError怎么用?Golang NewUnauthorizedError使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewUnauthorizedError函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: tryWebsocketConnection

func (c *Consumer) tryWebsocketConnection(path, token string) (*websocket.Conn, *httpError) {
	header := http.Header{"Origin": []string{"http://localhost"}, "Authorization": []string{token}}
	URL := c.trafficControllerUrl + path

	c.debugPrinter.Print("WEBSOCKET REQUEST:",
		"GET "+path+" HTTP/1.1\n"+
			"Host: "+c.trafficControllerUrl+"\n"+
			"Upgrade: websocket\nConnection: Upgrade\nSec-WebSocket-Version: 13\nSec-WebSocket-Key: [HIDDEN]\n"+
			headersString(header))

	ws, resp, err := c.dialer.Dial(URL, header)
	if resp != nil {
		c.debugPrinter.Print("WEBSOCKET RESPONSE:",
			resp.Proto+" "+resp.Status+"\n"+
				headersString(resp.Header))
	}

	httpErr := &httpError{}
	if resp != nil {
		if resp.StatusCode == http.StatusUnauthorized {
			bodyData, _ := ioutil.ReadAll(resp.Body)
			err = noaa_errors.NewUnauthorizedError(string(bodyData))
		}
		httpErr.statusCode = resp.StatusCode
	}
	if err != nil {
		errMsg := "Error dialing trafficcontroller server: %s.\n" +
			"Please ask your Cloud Foundry Operator to check the platform configuration (trafficcontroller is %s)."
		httpErr.error = fmt.Errorf(errMsg, err.Error(), c.trafficControllerUrl)
		return nil, httpErr
	}
	return ws, nil
}
开发者ID:fujitsu-cf,项目名称:cli,代码行数:33,代码来源:async.go

示例2: establishWebsocketConnection

func (c *Consumer) establishWebsocketConnection(path string, authToken string) (*websocket.Conn, error) {
	header := http.Header{"Origin": []string{"http://localhost"}, "Authorization": []string{authToken}}
	url := c.trafficControllerUrl + path

	c.debugPrinter.Print("WEBSOCKET REQUEST:",
		"GET "+path+" HTTP/1.1\n"+
			"Host: "+c.trafficControllerUrl+"\n"+
			"Upgrade: websocket\nConnection: Upgrade\nSec-WebSocket-Version: 13\nSec-WebSocket-Key: [HIDDEN]\n"+
			headersString(header))

	ws, resp, err := c.dialer.Dial(url, header)
	if resp != nil {
		c.debugPrinter.Print("WEBSOCKET RESPONSE:",
			resp.Proto+" "+resp.Status+"\n"+
				headersString(resp.Header))
	}

	if resp != nil && resp.StatusCode == http.StatusUnauthorized {
		bodyData, _ := ioutil.ReadAll(resp.Body)
		err = noaa_errors.NewUnauthorizedError(string(bodyData))
		return ws, err
	}

	callback := c.onConnectCallback()
	if err == nil && callback != nil {
		callback()
	}

	if err != nil {
		return nil, errors.New(fmt.Sprintf("Error dialing traffic controller server: %s.\nPlease ask your Cloud Foundry Operator to check the platform configuration (traffic controller is %s).", err.Error(), c.trafficControllerUrl))
	}

	return ws, err
}
开发者ID:Reejoshi,项目名称:cli,代码行数:34,代码来源:async.go

示例3: checkForErrors

func checkForErrors(resp *http.Response) error {
	if resp.StatusCode == http.StatusUnauthorized {
		data, _ := ioutil.ReadAll(resp.Body)
		return noaa_errors.NewUnauthorizedError(string(data))
	}

	if resp.StatusCode == http.StatusBadRequest {
		return ErrBadRequest
	}

	if resp.StatusCode != http.StatusOK {
		return ErrNotOK
	}
	return nil
}
开发者ID:shinji62,项目名称:firehose-to-fluentd,代码行数:15,代码来源:consumer.go

示例4: establishWebsocketConnection

func (cnsmr *consumer) establishWebsocketConnection(path string, authToken string) (*websocket.Conn, error) {
	header := http.Header{"Origin": []string{"http://localhost"}, "Authorization": []string{authToken}}

	dialer := websocket.Dialer{HandshakeTimeout: handshakeTimeout, NetDial: cnsmr.proxyDial, TLSClientConfig: cnsmr.tlsConfig}

	url := cnsmr.endpoint + path

	cnsmr.debugPrinter.Print("WEBSOCKET REQUEST:",
		"GET "+path+" HTTP/1.1\n"+
			"Host: "+cnsmr.endpoint+"\n"+
			"Upgrade: websocket\nConnection: Upgrade\nSec-WebSocket-Version: 13\nSec-WebSocket-Key: [HIDDEN]\n"+
			headersString(header))

	ws, resp, err := dialer.Dial(url, header)

	if resp != nil {
		cnsmr.debugPrinter.Print("WEBSOCKET RESPONSE:",
			resp.Proto+" "+resp.Status+"\n"+
				headersString(resp.Header))
	}

	if resp != nil && resp.StatusCode == http.StatusUnauthorized {
		bodyData, _ := ioutil.ReadAll(resp.Body)
		err = noaa_errors.NewUnauthorizedError(string(bodyData))
		return ws, err
	}

	if err == nil && cnsmr.callback != nil {
		cnsmr.callback()
	}

	if err != nil {
		return nil, errors.New(fmt.Sprintf("Error dialing loggregator server: %s.\nPlease ask your Cloud Foundry Operator to check the platform configuration (loggregator endpoint is %s).", err.Error(), cnsmr.endpoint))
	}

	return ws, err
}
开发者ID:Reejoshi,项目名称:cli,代码行数:37,代码来源:consumer.go

示例5: checkForErrors

func checkForErrors(resp *http.Response) *httpError {
	if resp.StatusCode == http.StatusUnauthorized {
		data, _ := ioutil.ReadAll(resp.Body)
		return &httpError{
			statusCode: resp.StatusCode,
			error:      noaa_errors.NewUnauthorizedError(string(data)),
		}
	}

	if resp.StatusCode == http.StatusBadRequest {
		return &httpError{
			statusCode: resp.StatusCode,
			error:      ErrBadRequest,
		}
	}

	if resp.StatusCode != http.StatusOK {
		return &httpError{
			statusCode: resp.StatusCode,
			error:      ErrNotOK,
		}
	}
	return nil
}
开发者ID:cloudfoundry,项目名称:v3-cli-plugin,代码行数:24,代码来源:consumer.go

示例6:

	BeforeEach(func() {
		fakeNoaaConsumer = &testapi.FakeNoaaConsumer{}
		config = testconfig.NewRepositoryWithDefaults()
		config.SetLoggregatorEndpoint("loggregator.test.com")
		config.SetDopplerEndpoint("doppler.test.com")
		config.SetAccessToken("the-access-token")
		fakeTokenRefresher = &testapi.FakeAuthenticationRepository{}
		repo = api.NewLogsNoaaRepository(config, fakeNoaaConsumer, fakeTokenRefresher)
	})

	Describe("RecentLogsFor", func() {

		It("refreshes token and get metric once more if token has expired.", func() {
			fakeNoaaConsumer.RecentLogsReturns([]*events.LogMessage{},
				noaa_errors.NewUnauthorizedError("Unauthorized token"))

			repo.RecentLogsFor("app-guid")
			Ω(fakeTokenRefresher.RefreshTokenCalled).To(BeTrue())
			Ω(fakeNoaaConsumer.RecentLogsCallCount()).To(Equal(2))
		})

		It("refreshes token and get metric once more if token has expired.", func() {
			fakeNoaaConsumer.RecentLogsReturns([]*events.LogMessage{}, errors.New("error error error"))

			_, err := repo.RecentLogsFor("app-guid")
			Ω(err).To(HaveOccurred())
			Ω(err.Error()).To(Equal("error error error"))
		})

		Context("when an error does not occur", func() {
开发者ID:tools-alexuser01,项目名称:cli,代码行数:30,代码来源:logs_noaa_test.go

示例7:

	})

	JustBeforeEach(func() {
		logsRepo = NewLoggregatorLogsRepository(configRepo, fakeConsumer, authRepo)
	})

	Describe("RecentLogsFor", func() {
		Context("when a noaa_errors.UnauthorizedError occurs", func() {
			var recentCalled bool
			BeforeEach(func() {
				fakeConsumer.RecentStub = func(string, string) ([]*logmessage.LogMessage, error) {
					if recentCalled {
						return nil, nil
					}
					recentCalled = true
					return nil, noaa_errors.NewUnauthorizedError("i'm sorry dave")
				}
			})

			It("refreshes the access token", func() {
				_, err := logsRepo.RecentLogsFor("app-guid")
				Expect(err).ToNot(HaveOccurred())
				Expect(authRepo.RefreshAuthTokenCallCount()).To(Equal(1))
			})
		})

		Context("when an error occurs", func() {
			BeforeEach(func() {
				fakeConsumer.RecentReturns(nil, errors.New("oops"))
			})
开发者ID:yingkitw,项目名称:cli,代码行数:30,代码来源:loggregator_logs_repository_test.go

示例8: httpRecent

/*
httpRecent connects to loggregator via its 'recent' http(s) endpoint and returns a slice of recent messages.
It does not guarantee any order of the messages; they are in the order returned by loggregator.
*/
func (cnsmr *consumer) httpRecent(appGuid string, authToken string) ([]*logmessage.LogMessage, error) {
	endpointUrl, err := url.ParseRequestURI(cnsmr.endpoint)
	if err != nil {
		return nil, err
	}

	scheme := "https"

	if endpointUrl.Scheme == "ws" {
		scheme = "http"
	}

	recentPath := fmt.Sprintf("%s://%s/recent?app=%s", scheme, endpointUrl.Host, appGuid)
	transport := &http.Transport{Proxy: cnsmr.proxy, TLSClientConfig: cnsmr.tlsConfig, TLSHandshakeTimeout: handshakeTimeout}
	client := &http.Client{Timeout: handshakeTimeout, Transport: transport}

	req, _ := http.NewRequest("GET", recentPath, nil)
	req.Header.Set("Authorization", authToken)

	resp, err := client.Do(req)
	if err != nil {
		return nil, errors.New(fmt.Sprintf("Error dialing loggregator server: %s.\nPlease ask your Cloud Foundry Operator to check the platform configuration (loggregator endpoint is %s).", err.Error(), cnsmr.endpoint))
	}
	defer resp.Body.Close()
	if resp.StatusCode == http.StatusUnauthorized {
		data, _ := ioutil.ReadAll(resp.Body)
		return nil, noaa_errors.NewUnauthorizedError(string(data))
	}

	if resp.StatusCode == http.StatusBadRequest {
		return nil, ErrBadRequest
	}

	if resp.StatusCode != http.StatusOK {
		return nil, ErrNotFound
	}

	contentType := resp.Header.Get("Content-Type")

	if len(strings.TrimSpace(contentType)) == 0 {
		return nil, ErrBadResponse
	}

	matches := boundaryRegexp.FindStringSubmatch(contentType)

	if len(matches) != 2 || len(strings.TrimSpace(matches[1])) == 0 {
		return nil, ErrBadResponse
	}

	reader := multipart.NewReader(resp.Body, matches[1])

	var buffer bytes.Buffer
	messages := make([]*logmessage.LogMessage, 0, 200)

	for part, loopErr := reader.NextPart(); loopErr == nil; part, loopErr = reader.NextPart() {
		buffer.Reset()

		msg := new(logmessage.LogMessage)
		_, err := buffer.ReadFrom(part)
		if err != nil {
			break
		}
		proto.Unmarshal(buffer.Bytes(), msg)
		messages = append(messages, msg)
	}

	return messages, err
}
开发者ID:Reejoshi,项目名称:cli,代码行数:72,代码来源:consumer.go

示例9:

		config.SetAccessToken("the-access-token")
		fakeTokenRefresher = &authenticationfakes.FakeRepository{}
		repo = logs.NewNoaaLogsRepository(config, fakeNoaaConsumer, fakeTokenRefresher)
	})

	Describe("RecentLogsFor", func() {
		It("refreshes token and get metric once more if token has expired.", func() {
			var recentLogsCallCount int

			fakeNoaaConsumer.RecentLogsStub = func(appGuid, authToken string) ([]*events.LogMessage, error) {
				defer func() {
					recentLogsCallCount += 1
				}()

				if recentLogsCallCount == 0 {
					return []*events.LogMessage{}, noaa_errors.NewUnauthorizedError("Unauthorized token")
				}

				return []*events.LogMessage{}, nil
			}

			repo.RecentLogsFor("app-guid")
			Expect(fakeTokenRefresher.RefreshAuthTokenCallCount()).To(Equal(1))
			Expect(fakeNoaaConsumer.RecentLogsCallCount()).To(Equal(2))
		})

		It("refreshes token and get metric once more if token has expired.", func() {
			fakeNoaaConsumer.RecentLogsReturns([]*events.LogMessage{}, errors.New("error error error"))

			_, err := repo.RecentLogsFor("app-guid")
			Expect(err).To(HaveOccurred())
开发者ID:jsloyer,项目名称:cli,代码行数:31,代码来源:noaa_logs_repository_test.go


注:本文中的github.com/cloudfoundry/noaa/errors.NewUnauthorizedError函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。