1# --------------------------------------------------------------------------
2#
3# Copyright (c) Microsoft Corporation. All rights reserved.
4#
5# The MIT License (MIT)
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the ""Software""), to
9# deal in the Software without restriction, including without limitation the
10# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11# sell copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23# IN THE SOFTWARE.
24#
25# --------------------------------------------------------------------------
26
27import json
28import logging
29
30
31from azure.core.exceptions import ODataV4Format
32
33
34_LOGGER = logging.getLogger(__name__)
35
36
37class TypedErrorInfo:
38 """Additional info class defined in ARM specification.
39
40 https://github.com/Azure/azure-resource-manager-rpc/blob/master/v1.0/common-api-details.md#error-response-content
41 """
42
43 def __init__(self, type, info): # pylint: disable=redefined-builtin
44 self.type = type
45 self.info = info
46
47 def __str__(self):
48 """Cloud error message."""
49 error_str = "Type: {}".format(self.type)
50 error_str += "\nInfo: {}".format(json.dumps(self.info, indent=4))
51 return error_str
52
53
54class ARMErrorFormat(ODataV4Format):
55 """Describe error format from ARM, used at the base or inside "details" node.
56
57 This format is compatible with ODataV4 format.
58 https://github.com/Azure/azure-resource-manager-rpc/blob/master/v1.0/common-api-details.md#error-response-content
59 """
60
61 def __init__(self, json_object):
62 # Parse the ODatav4 part
63 super(ARMErrorFormat, self).__init__(json_object)
64 if "error" in json_object:
65 json_object = json_object["error"]
66
67 # ARM specific annotations
68 self.additional_info = [
69 TypedErrorInfo(additional_info["type"], additional_info["info"])
70 for additional_info in json_object.get("additionalInfo", [])
71 ]
72
73 def __str__(self):
74 error_str = super(ARMErrorFormat, self).__str__()
75
76 if self.additional_info:
77 error_str += "\nAdditional Information:"
78 for error_info in self.additional_info:
79 error_str += str(error_info)
80
81 return error_str