github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/api/cloudcontroller/ccv2/errors_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccerror"
     7  	. "github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccv2"
     8  
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("Error Wrapper", func() {
    15  	var (
    16  		response           string
    17  		serverResponseCode int
    18  
    19  		client *Client
    20  	)
    21  
    22  	Describe("Make", func() {
    23  		BeforeEach(func() {
    24  			response = `{
    25  					"code": 777,
    26  					"description": "SomeCC Error Message",
    27  					"error_code": "CF-SomeError"
    28  				}`
    29  
    30  			client = NewTestClient()
    31  		})
    32  
    33  		JustBeforeEach(func() {
    34  			server.AppendHandlers(
    35  				CombineHandlers(
    36  					VerifyRequest(http.MethodGet, "/v2/apps"),
    37  					RespondWith(serverResponseCode, response, http.Header{
    38  						"X-Vcap-Request-Id": {
    39  							"6e0b4379-f5f7-4b2b-56b0-9ab7e96eed95",
    40  							"6e0b4379-f5f7-4b2b-56b0-9ab7e96eed95::7445d9db-c31e-410d-8dc5-9f79ec3fc26f",
    41  						},
    42  					},
    43  					),
    44  				),
    45  			)
    46  		})
    47  
    48  		Context("when the error is not from the cloud controller", func() {
    49  			BeforeEach(func() {
    50  				serverResponseCode = http.StatusTeapot
    51  				response = "418 I'm a teapot: Requested route ('some-url.com') does not exist."
    52  			})
    53  
    54  			It("returns a RawHTTPStatusError", func() {
    55  				_, _, err := client.GetApplications()
    56  				Expect(err).To(MatchError(ccerror.RawHTTPStatusError{
    57  					StatusCode:  http.StatusTeapot,
    58  					RawResponse: []byte(response),
    59  					RequestIDs: []string{
    60  						"6e0b4379-f5f7-4b2b-56b0-9ab7e96eed95",
    61  						"6e0b4379-f5f7-4b2b-56b0-9ab7e96eed95::7445d9db-c31e-410d-8dc5-9f79ec3fc26f",
    62  					},
    63  				}))
    64  			})
    65  		})
    66  
    67  		Context("when the error is from the cloud controller", func() {
    68  			Context("(400) Bad Request", func() {
    69  				BeforeEach(func() {
    70  					serverResponseCode = http.StatusBadRequest
    71  				})
    72  
    73  				Context("generic 400", func() {
    74  					BeforeEach(func() {
    75  						response = `{
    76  							"description": "bad request",
    77  							"error_code": "CF-BadRequest"
    78  						}`
    79  					})
    80  
    81  					It("returns a BadRequestError", func() {
    82  						_, _, err := client.GetApplications()
    83  						Expect(err).To(MatchError(ccerror.BadRequestError{
    84  							Message: "bad request",
    85  						}))
    86  					})
    87  				})
    88  
    89  				Context("when a not staged error is encountered", func() {
    90  					BeforeEach(func() {
    91  						response = `{
    92  								"description": "App has not finished staging",
    93  								"error_code": "CF-NotStaged"
    94  							}`
    95  					})
    96  
    97  					It("returns a NotStagedError", func() {
    98  						_, _, err := client.GetApplications()
    99  						Expect(err).To(MatchError(ccerror.NotStagedError{
   100  							Message: "App has not finished staging",
   101  						}))
   102  					})
   103  				})
   104  
   105  				Context("when an instances error is encountered", func() {
   106  					BeforeEach(func() {
   107  						response = `{
   108  								"description": "instances went bananas",
   109  								"error_code": "CF-InstancesError"
   110  							}`
   111  					})
   112  
   113  					It("returns an InstancesError", func() {
   114  						_, _, err := client.GetApplications()
   115  						Expect(err).To(MatchError(ccerror.InstancesError{
   116  							Message: "instances went bananas",
   117  						}))
   118  					})
   119  				})
   120  
   121  				Context("when creating a relation that is invalid", func() {
   122  					BeforeEach(func() {
   123  						response = `{
   124  							"code": 1002,
   125  							"description": "The requested app relation is invalid: the app and route must belong to the same space",
   126  							"error_code": "CF-InvalidRelation"
   127  						}`
   128  					})
   129  
   130  					It("returns an InvalidRelationError", func() {
   131  						_, _, err := client.GetApplications()
   132  						Expect(err).To(MatchError(ccerror.InvalidRelationError{
   133  							Message: "The requested app relation is invalid: the app and route must belong to the same space",
   134  						}))
   135  					})
   136  				})
   137  
   138  				Context("getting stats for a stopped app", func() {
   139  					BeforeEach(func() {
   140  						response = `{
   141  							"code": 200003,
   142  							"description": "Could not fetch stats for stopped app: some-app",
   143  							"error_code": "CF-AppStoppedStatsError"
   144  						}`
   145  					})
   146  
   147  					It("returns an AppStoppedStatsError", func() {
   148  						_, _, err := client.GetApplications()
   149  						Expect(err).To(MatchError(ccerror.ApplicationStoppedStatsError{
   150  							Message: "Could not fetch stats for stopped app: some-app",
   151  						}))
   152  					})
   153  				})
   154  			})
   155  
   156  			Context("(401) Unauthorized", func() {
   157  				BeforeEach(func() {
   158  					serverResponseCode = http.StatusUnauthorized
   159  				})
   160  
   161  				Context("generic 401", func() {
   162  					It("returns a UnauthorizedError", func() {
   163  						_, _, err := client.GetApplications()
   164  						Expect(err).To(MatchError(ccerror.UnauthorizedError{Message: "SomeCC Error Message"}))
   165  					})
   166  				})
   167  
   168  				Context("invalid token", func() {
   169  					BeforeEach(func() {
   170  						response = `{
   171  						"code": 1000,
   172  						"description": "Invalid Auth Token",
   173  						"error_code": "CF-InvalidAuthToken"
   174  					}`
   175  					})
   176  
   177  					It("returns an InvalidAuthTokenError", func() {
   178  						_, _, err := client.GetApplications()
   179  						Expect(err).To(MatchError(ccerror.InvalidAuthTokenError{Message: "Invalid Auth Token"}))
   180  					})
   181  				})
   182  			})
   183  
   184  			Context("(403) Forbidden", func() {
   185  				BeforeEach(func() {
   186  					serverResponseCode = http.StatusForbidden
   187  				})
   188  
   189  				It("returns a ForbiddenError", func() {
   190  					_, _, err := client.GetApplications()
   191  					Expect(err).To(MatchError(ccerror.ForbiddenError{Message: "SomeCC Error Message"}))
   192  				})
   193  			})
   194  
   195  			Context("(404) Not Found", func() {
   196  				BeforeEach(func() {
   197  					serverResponseCode = http.StatusNotFound
   198  				})
   199  
   200  				Context("when the error is a json response from the cloud controller", func() {
   201  					It("returns a ResourceNotFoundError", func() {
   202  						_, _, err := client.GetApplications()
   203  						Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: "SomeCC Error Message"}))
   204  					})
   205  				})
   206  
   207  				Context("when the error is not from the cloud controller API", func() {
   208  					BeforeEach(func() {
   209  						response = "an error not from the CC API"
   210  					})
   211  
   212  					It("returns a NotFoundError", func() {
   213  						_, _, err := client.GetApplications()
   214  						Expect(err).To(MatchError(ccerror.NotFoundError{Message: response}))
   215  					})
   216  				})
   217  			})
   218  
   219  			Context("(422) Unprocessable Entity", func() {
   220  				BeforeEach(func() {
   221  					serverResponseCode = http.StatusUnprocessableEntity
   222  				})
   223  
   224  				It("returns a UnprocessableEntityError", func() {
   225  					_, _, err := client.GetApplications()
   226  					Expect(err).To(MatchError(ccerror.UnprocessableEntityError{Message: "SomeCC Error Message"}))
   227  				})
   228  			})
   229  
   230  			Context("unhandled Error Codes", func() {
   231  				BeforeEach(func() {
   232  					serverResponseCode = http.StatusTeapot
   233  				})
   234  
   235  				It("returns an UnexpectedResponseError", func() {
   236  					_, _, err := client.GetApplications()
   237  					Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   238  						ResponseCode: http.StatusTeapot,
   239  						V2ErrorResponse: ccerror.V2ErrorResponse{
   240  							Code:        777,
   241  							Description: "SomeCC Error Message",
   242  							ErrorCode:   "CF-SomeError",
   243  						},
   244  						RequestIDs: []string{
   245  							"6e0b4379-f5f7-4b2b-56b0-9ab7e96eed95",
   246  							"6e0b4379-f5f7-4b2b-56b0-9ab7e96eed95::7445d9db-c31e-410d-8dc5-9f79ec3fc26f",
   247  						},
   248  					}))
   249  				})
   250  			})
   251  		})
   252  	})
   253  })