Skip to content

Generic error interface to get status code For #1632#1633

Closed
snigle wants to merge 1 commit intogophercloud:masterfrom
snigle:patch-1
Closed

Generic error interface to get status code For #1632#1633
snigle wants to merge 1 commit intogophercloud:masterfrom
snigle:patch-1

Conversation

@snigle
Copy link
Copy Markdown
Contributor

@snigle snigle commented Jun 25, 2019

For #1632

Permit to handle gopher error easily of need to just return a status code.
Eg :

gophError := err.(gophercloud.GenericError)
fmt.Println(gophError.GetStatusCode()) 

@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.01%) to 76.834% when pulling 9ac3d14 on snigle:patch-1 into 1574321 on gophercloud:master.

2 similar comments
@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.01%) to 76.834% when pulling 9ac3d14 on snigle:patch-1 into 1574321 on gophercloud:master.

@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.01%) to 76.834% when pulling 9ac3d14 on snigle:patch-1 into 1574321 on gophercloud:master.

@coveralls
Copy link
Copy Markdown

coveralls commented Jun 25, 2019

Coverage Status

Coverage increased (+0.003%) to 76.863% when pulling 2b33e88 on snigle:patch-1 into a26f5d9 on gophercloud:master.

@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Jun 25, 2019

Build succeeded.

@jtopjian
Copy link
Copy Markdown
Contributor

@snigle Thank you for working on this.

Are you sure an interface is needed? Since all ErrDefaultNNN error types inherit from ErrUnexpectedResponseCode, then just having the GetStatusCode method will work without the need for an interface.

Also, would you be able to add some unit tests for this PR? Something like the following under a new file called gophercloud/testing/errors_test.go is a good start:

package testing

import (
  "testing"

  "github.com/gophercloud/gophercloud"
  th "github.com/gophercloud/gophercloud/testhelper"
)

func TestGetResponseCode(t *testing.T) {
  respErr := gophercloud.ErrUnexpectedResponseCode{
    URL:      "http://example.com",
    Method:   "GET",
    Expected: []int{200},
    Actual:   404,
    Body:     nil,
  }

  err404 := gophercloud.ErrDefault404{respErr}

  th.AssertEquals(t, err404.GetStatusCode(), 404)
}

@snigle snigle changed the title Generic error interface to get status code For #1632 [wip] Generic error interface to get status code For #1632 Jul 5, 2019
@snigle
Copy link
Copy Markdown
Contributor Author

snigle commented Jul 5, 2019

@snigle Thank you for working on this.

Are you sure an interface is needed? Since all ErrDefaultNNN error types inherit from ErrUnexpectedResponseCode, then just having the GetStatusCode method will work without the need for an interface.

Also, would you be able to add some unit tests for this PR? Something like the following under a new file called gophercloud/testing/errors_test.go is a good start:

package testing

import (
  "testing"

  "github.com/gophercloud/gophercloud"
  th "github.com/gophercloud/gophercloud/testhelper"
)

func TestGetResponseCode(t *testing.T) {
  respErr := gophercloud.ErrUnexpectedResponseCode{
    URL:      "http://example.com",
    Method:   "GET",
    Expected: []int{200},
    Actual:   404,
    Body:     nil,
  }

  err404 := gophercloud.ErrDefault404{respErr}

  th.AssertEquals(t, err404.GetStatusCode(), 404)
}

I need the interface because gophercloud return error interface and not ErrDefault404 type. Added the test file thanks.

@snigle snigle changed the title [wip] Generic error interface to get status code For #1632 Generic error interface to get status code For #1632 Jul 5, 2019
@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Jul 5, 2019

Build succeeded.

@jtopjian
Copy link
Copy Markdown
Contributor

jtopjian commented Jul 5, 2019

I need the interface because gophercloud return error interface and not ErrDefault404 type

Right, but you can cast any error to the common ErrUnexpectedResponseCode which would act the same as an interface. If this won't work for you, can you provide some example code that uses the interface so I can better understand the situation?

@jtopjian
Copy link
Copy Markdown
Contributor

Superseded by #1820

@jtopjian jtopjian closed this Jan 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants