@@ -2,8 +2,12 @@ package apps
22
33import (
44 "errors"
5+ "fmt"
56 "testing"
67
8+ "github.com/databricks/databricks-sdk-go/apierr"
9+ "github.com/databricks/databricks-sdk-go/retries"
10+ "github.com/spf13/cobra"
711 "github.com/stretchr/testify/assert"
812 "github.com/stretchr/testify/require"
913)
@@ -52,3 +56,113 @@ func TestAppDeploymentError_Unwrap(t *testing.T) {
5256 require .Equal (t , originalErr , unwrapped )
5357 assert .ErrorIs (t , appErr , originalErr , "errors.Is should work with wrapped error" )
5458}
59+
60+ func TestWrapDeploymentError (t * testing.T ) {
61+ tests := []struct {
62+ name string
63+ err error
64+ appName string
65+ expectWrapped bool
66+ description string
67+ }{
68+ {
69+ name : "nil error" ,
70+ err : nil ,
71+ appName : "test-app" ,
72+ expectWrapped : false ,
73+ description : "nil error should return nil unchanged" ,
74+ },
75+ {
76+ name : "plain error without retries wrapper" ,
77+ err : errors .New ("some error" ),
78+ appName : "test-app" ,
79+ expectWrapped : false ,
80+ description : "plain errors should not be wrapped" ,
81+ },
82+ {
83+ name : "retries.Err with Halt=false" ,
84+ err : retries .Continues ("still in progress" ),
85+ appName : "test-app" ,
86+ expectWrapped : false ,
87+ description : "transient retries errors should not be wrapped" ,
88+ },
89+ {
90+ name : "retries.Err with 404 API error (not found)" ,
91+ err : retries .Halt (fmt .Errorf ("API error: %w" , & apierr.APIError {
92+ StatusCode : 404 ,
93+ ErrorCode : "NOT_FOUND" ,
94+ Message : "App with name test-app does not exist or is deleted." ,
95+ })),
96+ appName : "test-app" ,
97+ expectWrapped : false ,
98+ description : "404 not found errors should not be wrapped with logs hint" ,
99+ },
100+ {
101+ name : "retries.Err with 400 API error (bad request)" ,
102+ err : retries .Halt (fmt .Errorf ("API error: %w" , & apierr.APIError {
103+ StatusCode : 400 ,
104+ ErrorCode : "BAD_REQUEST" ,
105+ Message : "Invalid request parameters" ,
106+ })),
107+ appName : "test-app" ,
108+ expectWrapped : false ,
109+ description : "400 bad request errors should not be wrapped with logs hint" ,
110+ },
111+ {
112+ name : "retries.Err with 403 API error (forbidden)" ,
113+ err : retries .Halt (fmt .Errorf ("API error: %w" , & apierr.APIError {
114+ StatusCode : 403 ,
115+ ErrorCode : "FORBIDDEN" ,
116+ Message : "Access denied" ,
117+ })),
118+ appName : "test-app" ,
119+ expectWrapped : false ,
120+ description : "403 forbidden errors should not be wrapped with logs hint" ,
121+ },
122+ {
123+ name : "retries.Err with 500 API error (server error)" ,
124+ err : retries .Halt (fmt .Errorf ("API error: %w" , & apierr.APIError {
125+ StatusCode : 500 ,
126+ ErrorCode : "INTERNAL_ERROR" ,
127+ Message : "Internal server error" ,
128+ })),
129+ appName : "test-app" ,
130+ expectWrapped : true ,
131+ description : "500 server errors during wait should be wrapped with logs hint" ,
132+ },
133+ {
134+ name : "retries.Err without API error (deployment failure)" ,
135+ err : retries .Halt (errors .New ("failed to reach SUCCEEDED, got FAILED: Error building app" )),
136+ appName : "test-app" ,
137+ expectWrapped : true ,
138+ description : "deployment failures should be wrapped with logs hint" ,
139+ },
140+ {
141+ name : "retries.Err without API error (timeout)" ,
142+ err : retries .Halt (errors .New ("timeout waiting for deployment" )),
143+ appName : "test-app" ,
144+ expectWrapped : true ,
145+ description : "timeout errors should be wrapped with logs hint" ,
146+ },
147+ }
148+
149+ for _ , tt := range tests {
150+ t .Run (tt .name , func (t * testing.T ) {
151+ cmd := & cobra.Command {}
152+ result := wrapDeploymentError (cmd , tt .appName , tt .err )
153+
154+ if tt .expectWrapped {
155+ var appErr * AppDeploymentError
156+ require .ErrorAs (t , result , & appErr , tt .description )
157+ assert .Equal (t , tt .appName , appErr .appName )
158+ assert .ErrorIs (t , result , tt .err , "wrapped error should unwrap to original" )
159+ assert .Contains (t , result .Error (), "To view app logs, run:" , "should contain logs hint" )
160+ } else {
161+ assert .Equal (t , tt .err , result , tt .description )
162+ if tt .err != nil {
163+ assert .NotContains (t , result .Error (), "To view app logs" , "should not contain logs hint" )
164+ }
165+ }
166+ })
167+ }
168+ }
0 commit comments