~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/Azure/azure-sdk-for-go/management/errors.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package management
 
2
 
 
3
import (
 
4
        "encoding/xml"
 
5
        "fmt"
 
6
)
 
7
 
 
8
// AzureError represents an error returned by the management API. It has an error
 
9
// code (for example, ResourceNotFound) and a descriptive message.
 
10
type AzureError struct {
 
11
        Code    string
 
12
        Message string
 
13
}
 
14
 
 
15
//Error implements the error interface for the AzureError type.
 
16
func (e AzureError) Error() string {
 
17
        return fmt.Sprintf("Error response from Azure. Code: %s, Message: %s", e.Code, e.Message)
 
18
}
 
19
 
 
20
// IsResourceNotFoundError returns true if the provided error is an AzureError
 
21
// reporting that a given resource has not been found.
 
22
func IsResourceNotFoundError(err error) bool {
 
23
        azureErr, ok := err.(AzureError)
 
24
        return ok && azureErr.Code == "ResourceNotFound"
 
25
}
 
26
 
 
27
// getAzureError converts an error response body into an AzureError instance.
 
28
func getAzureError(responseBody []byte) error {
 
29
        var azErr AzureError
 
30
        err := xml.Unmarshal(responseBody, &azErr)
 
31
        if err != nil {
 
32
                return fmt.Errorf("Failed parsing contents to AzureError format: %v", err)
 
33
        }
 
34
        return azErr
 
35
 
 
36
}