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