@@ -64,6 +64,51 @@ func TestError(t *testing.T) {
6464 _ = fmt .Sprintf ("%s" , err )
6565 })
6666
67+ t .Run ("CausedBy" , func (t * testing.T ) {
68+ resp := & opensearch.Response {
69+ StatusCode : http .StatusBadRequest ,
70+ Body : io .NopCloser (
71+ strings .NewReader (`{
72+ "error":{
73+ "root_cause":[{
74+ "type":"illegal_argument_exception",
75+ "reason":"composable template [posts] template after composition is invalid"
76+ }],
77+ "type":"illegal_argument_exception",
78+ "reason":"composable template [posts] template after composition is invalid",
79+ "caused_by":{
80+ "type":"illegal_argument_exception",
81+ "reason":"Custom analyzer [mm_analyzer] failed to find filter under name [test_filter]",
82+ "caused_by":{
83+ "type":"illegal_argument_exception",
84+ "reason":"test caused by"
85+ }
86+ }
87+ },
88+ "status":400
89+ }` ),
90+ ),
91+ }
92+ assert .True (t , resp .IsError ())
93+ err := opensearch .ParseError (resp )
94+ var testError * opensearch.StructError
95+ require .True (t , errors .As (err , & testError ))
96+ assert .Equal (t , http .StatusBadRequest , testError .Status )
97+ assert .Equal (t , "illegal_argument_exception" , testError .Err .Type )
98+ assert .Equal (t , "composable template [posts] template after composition is invalid" , testError .Err .Reason )
99+ assert .NotNil (t , testError .Err .RootCause )
100+ assert .Equal (t , "illegal_argument_exception" , testError .Err .RootCause [0 ].Type )
101+ assert .Equal (t , "composable template [posts] template after composition is invalid" , testError .Err .RootCause [0 ].Reason )
102+ assert .NotNil (t , testError .Err .CausedBy )
103+ assert .Equal (t , "illegal_argument_exception" , testError .Err .CausedBy .Type )
104+ assert .Equal (t , "Custom analyzer [mm_analyzer] failed to find filter under name [test_filter]" , testError .Err .CausedBy .Reason )
105+ assert .NotNil (t , testError .Err .CausedBy .CausedBy )
106+ assert .Equal (t , "illegal_argument_exception" , testError .Err .CausedBy .CausedBy .Type )
107+ assert .Equal (t , "test caused by" , testError .Err .CausedBy .CausedBy .Reason )
108+ assert .Nil (t , testError .Err .CausedBy .CausedBy .CausedBy )
109+ _ = fmt .Sprintf ("%s" , err )
110+ })
111+
67112 t .Run ("Unmarshal errors" , func (t * testing.T ) {
68113 t .Run ("dummy" , func (t * testing.T ) {
69114 reader := io .NopCloser (
0 commit comments