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# --------------------------------------------------------------------------
26from typing import Mapping, Any, Sequence
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: str, info: Mapping[str, Any]) -> None: # pylint: disable=redefined-builtin
44 self.type = type
45 self.info = info
46
47 def __str__(self) -> str:
48 """Cloud error message.
49
50 :return: The cloud error message.
51 :rtype: str
52 """
53 error_str = "Type: {}".format(self.type)
54 error_str += "\nInfo: {}".format(json.dumps(self.info, indent=4))
55 return error_str
56
57
58class ARMErrorFormat(ODataV4Format):
59 """Describe error format from ARM, used at the base or inside "details" node.
60
61 This format is compatible with ODataV4 format.
62 https://github.com/Azure/azure-resource-manager-rpc/blob/master/v1.0/common-api-details.md#error-response-content
63 """
64
65 def __init__(self, json_object: Mapping[str, Any]) -> None:
66 # Parse the ODatav4 part
67 super(ARMErrorFormat, self).__init__(json_object)
68 if "error" in json_object:
69 json_object = json_object["error"]
70
71 # ARM specific annotations
72 self.additional_info: Sequence[TypedErrorInfo] = [
73 TypedErrorInfo(additional_info["type"], additional_info["info"])
74 for additional_info in json_object.get("additionalInfo", [])
75 ]
76
77 def __str__(self) -> str:
78 error_str = super(ARMErrorFormat, self).__str__()
79
80 if self.additional_info:
81 error_str += "\nAdditional Information:"
82 for error_info in self.additional_info:
83 error_str += str(error_info)
84
85 return error_str