@@ -2,6 +2,7 @@ package curl
22
33import (
44 "bytes"
5+ "encoding/json"
56 "fmt"
67 "io"
78 "net/http"
@@ -104,6 +105,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
104105 }
105106
106107 if model .FailOnHTTPError && resp .StatusCode >= 400 {
108+ fmt .Println ("failed!!" )
107109 os .Exit (22 )
108110 }
109111 return nil
@@ -170,11 +172,29 @@ func getBearerToken(p *print.Printer) (string, error) {
170172 p .Debug (print .ErrorLevel , "configure authentication: %v" , err )
171173 return "" , & errors.AuthError {}
172174 }
173- token , err := auth .GetAuthField (auth .ACCESS_TOKEN )
175+
176+ userSessionExpired , err := auth .UserSessionExpired ()
177+ if err != nil {
178+ return "" , err
179+ }
180+ if userSessionExpired {
181+ return "" , & errors.SessionExpiredError {}
182+ }
183+
184+ accessToken , err := auth .GetAccessToken ()
174185 if err != nil {
175- return "" , fmt .Errorf ("get access token: %w" , err )
186+ return "" , err
187+ }
188+
189+ accessTokenExpired , err := auth .TokenExpired (accessToken )
190+ if err != nil {
191+ return "" , err
192+ }
193+ if accessTokenExpired {
194+ return "" , & errors.AccessTokenExpiredError {}
176195 }
177- return token , nil
196+
197+ return accessToken , nil
178198}
179199
180200func buildRequest (model * inputModel , bearerToken string ) (* http.Request , error ) {
@@ -213,6 +233,22 @@ func outputResponse(p *print.Printer, model *inputModel, resp *http.Response) er
213233 if err != nil {
214234 return fmt .Errorf ("read response body: %w" , err )
215235 }
236+
237+ if strings .Contains (strings .ToLower (string (respBody )), "jwt is expired" ) {
238+ return & errors.SessionExpiredError {}
239+ }
240+
241+ if strings .Contains (strings .ToLower (string (respBody )), "jwt is missing" ) {
242+ return & errors.AuthError {}
243+ }
244+
245+ var prettyJSON bytes.Buffer
246+ if json .Valid (respBody ) {
247+ if err := json .Indent (& prettyJSON , respBody , "" , " " ); err == nil {
248+ respBody = prettyJSON .Bytes ()
249+ } // if indenting fails, fall back to original body
250+ }
251+
216252 output = append (output , respBody ... )
217253
218254 if model .OutputFile == nil {
0 commit comments