-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: centralized error handling to be more central (#406)
- Loading branch information
1 parent
1851d9d
commit 632e02f
Showing
8 changed files
with
141 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package amterror | ||
|
||
import ( | ||
"encoding/xml" | ||
"fmt" | ||
) | ||
|
||
func (e *AMTError) Error() string { | ||
return fmt.Sprintf("Error [SubCode: %s] Message: %s, Detail: %s", e.SubCode, e.Message, e.Detail) | ||
} | ||
|
||
func NewAMTError(subCode, message, detail string) *AMTError { | ||
return &AMTError{ | ||
SubCode: subCode, | ||
Message: message, | ||
Detail: detail, | ||
} | ||
} | ||
|
||
func DecodeAMTErrorString(s string) error { | ||
checkForErrorResponse := ErrorResponse{} | ||
|
||
err := xml.Unmarshal([]byte(s), &checkForErrorResponse) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return NewAMTError(checkForErrorResponse.Body.Fault.Code.SubCode.Value, checkForErrorResponse.Body.Fault.Reason.Text, checkForErrorResponse.Body.Fault.Detail) | ||
} | ||
|
||
// AMT WSMAN Error Response Types. | ||
type ( | ||
AMTError struct { | ||
SubCode string | ||
Message string | ||
Detail string | ||
} | ||
|
||
Header struct { | ||
XMLName xml.Name `xml:"Header"` | ||
To string `xml:"To"` | ||
RelatesTo int `xml:"RelatesTo"` | ||
Action Action `xml:"Action"` | ||
MessageID string `xml:"MessageID"` | ||
ResourceURI string `xml:"ResourceURI"` | ||
} | ||
|
||
Action struct { | ||
XMLName xml.Name `xml:"Action"` | ||
MustUnderstand string `xml:"mustUnderstand,attr"` | ||
Value string `xml:",chardata"` | ||
} | ||
|
||
ErrorResponse struct { | ||
XMLName xml.Name `xml:"Envelope"` | ||
Header Header `xml:"Header"` | ||
Body ErrorBody `xml:"Body"` | ||
} | ||
|
||
ErrorBody struct { | ||
XMLName xml.Name `xml:"Body"` | ||
Fault Fault `xml:"Fault"` | ||
} | ||
|
||
Fault struct { | ||
XMLName xml.Name `xml:"Fault"` | ||
Code Code `xml:"Code"` | ||
Reason Reason `xml:"Reason"` | ||
Detail string `xml:"Detail"` | ||
} | ||
|
||
Code struct { | ||
XMLName xml.Name `xml:"Code"` | ||
Value string `xml:"Value"` | ||
SubCode SubCode `xml:"Subcode"` | ||
} | ||
|
||
SubCode struct { | ||
XMLName xml.Name `xml:"Subcode"` | ||
Value string `xml:"Value"` | ||
} | ||
|
||
Reason struct { | ||
XMLName xml.Name `xml:"Reason"` | ||
Text string `xml:"Text"` | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/********************************************************************* | ||
* Copyright (c) Intel Corporation 2024 | ||
* SPDX-License-Identifier: Apache-2.0 | ||
**********************************************************************/ | ||
|
||
package amterror | ||
|
||
import ( | ||
"encoding/xml" | ||
"testing" | ||
) | ||
|
||
func TestDecodeWSMANError(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected error | ||
}{ | ||
{ | ||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:Envelope xmlns:g=\"http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd\" xmlns:f=\"http://schemas.xmlsoap.org/ws/2004/08/eventing\" xmlns:e=\"http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd\" xmlns:d=\"http://schemas.xmlsoap.org/ws/2004/09/transfer\" xmlns:c=\"http://schemas.xmlsoap.org/ws/2004/09/enumeration\" xmlns:b=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:a=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:h=\"http://schemas.xmlsoap.org/ws/2005/02/trust\" xmlns:i=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><a:Header><b:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</b:To><b:RelatesTo>0</b:RelatesTo><b:Action a:mustUnderstand=\"true\">http://schemas.xmlsoap.org/ws/2004/08/addressing/fault</b:Action><b:MessageID>uuid:00000000-8086-8086-8086-000000000061</b:MessageID></a:Header><a:Body><a:Fault><a:Code><a:Value>a:Sender</a:Value><a:Subcode><a:Value>b:DestinationUnreachable</a:Value></a:Subcode></a:Code><a:Reason><a:Text xml:lang=\"en-US\">No route can be determined to reach the destination role defined by the WSAddressing To.</a:Text></a:Reason><a:Detail></a:Detail></a:Fault></a:Body></a:Envelope>", | ||
NewAMTError("b:DestinationUnreachable", "No route can be determined to reach the destination role defined by the WSAddressing To.", ""), | ||
}, | ||
{ | ||
"bad xml", | ||
xml.Unmarshal([]byte("bad xml"), &ErrorResponse{}), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
result := DecodeAMTErrorString(test.input) | ||
if result == nil { | ||
if test.expected != nil { | ||
t.Errorf("Expected %s, but got nil", test.expected) | ||
} | ||
} else { | ||
if result.Error() != test.expected.Error() { | ||
t.Errorf("Expected %s, but got %s", test.expected, result) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters