Skip to content

Commit

Permalink
Improve error handling. (#8)
Browse files Browse the repository at this point in the history
* Improve golang error handing part 1

* Fix generic error

* Improve usability of errors

* Documentation

* Documentation

* Docs

* Version bump
  • Loading branch information
philcluff authored Oct 25, 2019
1 parent 9a3f093 commit 0e6e71c
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 527 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ func main() {
}
```

## Errors & Error Handling

All API calls return an err as their final return value. Below is documented the errors you might want to check for. You can check `error.Body` on all errors to see the full HTTP response.

### BadRequestError

`BadRequestError` is returned when a you make a bad request to Mux, this likely means you've passed in an invalid parameter to the API call.

### UnauthorizedError

`UnauthorizedError` is returned when Mux cannot authenticate your request. [You should check you have configured your credentials correctly.](#authentication)

### ForbiddenError

`ForbiddenError` is returned you don't have permission to access that resource. [You should check you have configured your credentials correctly.](#authentication)

### NotFoundError

`NotFoundError` is returned when a resource is not found. This is useful when trying to get an entity by its ID.

### TooManyRequestsError

`TooManyRequestsError` is returned when you exceed the manimum request that Mux allows. Please get in touch with [[email protected]](mailto:[email protected]) if you need to talk about this limit.

### ServiceError

`ServiceError` is returned when Mux returns a HTTP 5XX Status Code. If you encounter this reproducibly, please get in touch with [[email protected]](mailto:[email protected]).

### GenericOpenAPIError

`GenericOpenAPIError` is a fallback Error which may be returned in some edge cases. This will be deprecated in a later release but remains present for API compatibility.

## Documentation

[Be sure to check out the documentation in the `docs` directory.](docs/)
Expand Down
160 changes: 36 additions & 124 deletions api_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,10 @@ func (a *AssetsApiService) CreateAsset(createAssetRequest CreateAssetRequest, op
return localVarReturnValue, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 200 {
var v AssetResponse
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, newErr
}
newErr.model = v
return localVarReturnValue, newErr
}
return localVarReturnValue, newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return localVarReturnValue, err
}

err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
Expand Down Expand Up @@ -159,22 +147,10 @@ func (a *AssetsApiService) CreateAssetPlaybackId(aSSETID string, createPlaybackI
return localVarReturnValue, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 201 {
var v CreatePlaybackIdResponse
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, newErr
}
newErr.model = v
return localVarReturnValue, newErr
}
return localVarReturnValue, newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return localVarReturnValue, err
}

err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
Expand Down Expand Up @@ -245,12 +221,10 @@ func (a *AssetsApiService) DeleteAsset(aSSETID string, opts ...APIOption) error
return err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
return newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return err
}

return nil
Expand Down Expand Up @@ -313,12 +287,10 @@ func (a *AssetsApiService) DeleteAssetPlaybackId(aSSETID string, pLAYBACKID stri
return err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
return newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return err
}

return nil
Expand Down Expand Up @@ -381,22 +353,10 @@ func (a *AssetsApiService) GetAsset(aSSETID string, opts ...APIOption) (AssetRes
return localVarReturnValue, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 200 {
var v AssetResponse
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, newErr
}
newErr.model = v
return localVarReturnValue, newErr
}
return localVarReturnValue, newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return localVarReturnValue, err
}

err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
Expand Down Expand Up @@ -468,22 +428,10 @@ func (a *AssetsApiService) GetAssetInputInfo(aSSETID string, opts ...APIOption)
return localVarReturnValue, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 200 {
var v GetAssetInputInfoResponse
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, newErr
}
newErr.model = v
return localVarReturnValue, newErr
}
return localVarReturnValue, newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return localVarReturnValue, err
}

err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
Expand Down Expand Up @@ -556,22 +504,10 @@ func (a *AssetsApiService) GetAssetPlaybackId(aSSETID string, pLAYBACKID string,
return localVarReturnValue, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 200 {
var v GetAssetPlaybackIdResponse
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, newErr
}
newErr.model = v
return localVarReturnValue, newErr
}
return localVarReturnValue, newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return localVarReturnValue, err
}

err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
Expand Down Expand Up @@ -659,22 +595,10 @@ func (a *AssetsApiService) ListAssets(opts ...APIOption) (ListAssetsResponse, er
return localVarReturnValue, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 200 {
var v ListAssetsResponse
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, newErr
}
newErr.model = v
return localVarReturnValue, newErr
}
return localVarReturnValue, newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return localVarReturnValue, err
}

err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
Expand Down Expand Up @@ -748,22 +672,10 @@ func (a *AssetsApiService) UpdateAssetMp4Support(aSSETID string, updateAssetMp4S
return localVarReturnValue, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 200 {
var v AssetResponse
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, newErr
}
newErr.model = v
return localVarReturnValue, newErr
}
return localVarReturnValue, newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return localVarReturnValue, err
}

err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
Expand Down
80 changes: 16 additions & 64 deletions api_direct_uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,10 @@ func (a *DirectUploadsApiService) CancelDirectUpload(uPLOADID string, opts ...AP
return localVarReturnValue, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 200 {
var v UploadResponse
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, newErr
}
newErr.model = v
return localVarReturnValue, newErr
}
return localVarReturnValue, newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return localVarReturnValue, err
}

err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
Expand Down Expand Up @@ -157,22 +145,10 @@ func (a *DirectUploadsApiService) CreateDirectUpload(createUploadRequest CreateU
return localVarReturnValue, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 201 {
var v UploadResponse
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, newErr
}
newErr.model = v
return localVarReturnValue, newErr
}
return localVarReturnValue, newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return localVarReturnValue, err
}

err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
Expand Down Expand Up @@ -244,22 +220,10 @@ func (a *DirectUploadsApiService) GetDirectUpload(uPLOADID string, opts ...APIOp
return localVarReturnValue, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 200 {
var v UploadResponse
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, newErr
}
newErr.model = v
return localVarReturnValue, newErr
}
return localVarReturnValue, newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return localVarReturnValue, err
}

err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
Expand Down Expand Up @@ -347,22 +311,10 @@ func (a *DirectUploadsApiService) ListDirectUploads(opts ...APIOption) (ListUplo
return localVarReturnValue, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 200 {
var v ListUploadsResponse
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, newErr
}
newErr.model = v
return localVarReturnValue, newErr
}
return localVarReturnValue, newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return localVarReturnValue, err
}

err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
Expand Down
20 changes: 4 additions & 16 deletions api_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,10 @@ func (a *ErrorsApiService) ListErrors(opts ...APIOption) (ListErrorsResponse, er
return localVarReturnValue, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 200 {
var v ListErrorsResponse
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, newErr
}
newErr.model = v
return localVarReturnValue, newErr
}
return localVarReturnValue, newErr
// Check for common HTTP error status codes
err = CheckForHttpError(localVarHttpResponse.StatusCode, localVarBody)
if err != nil {
return localVarReturnValue, err
}

err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
Expand Down
Loading

0 comments on commit 0e6e71c

Please sign in to comment.