@@ -13,7 +13,12 @@ import (
1313 jsoniter "github.com/json-iterator/go"
1414)
1515
16- func GetUUID (username string ) (string , error ) {
16+ func GetUUID (username string , throwAnError ... bool ) (string , error ) {
17+ shouldThrowError := true
18+ if len (throwAnError ) > 0 {
19+ shouldThrowError = throwAnError [0 ]
20+ }
21+
1722 var post models.MowojangReponse
1823
1924 cache , err := redis .Get (fmt .Sprintf ("uuid:%s" , strings .ToLower (username )))
@@ -23,23 +28,43 @@ func GetUUID(username string) (string, error) {
2328
2429 resp , err := http .Get (fmt .Sprintf ("https://mowojang.matdoes.dev/%s" , username ))
2530 if err != nil {
26- return post .UUID , fmt .Errorf ("error making request: %v" , err )
31+ if shouldThrowError {
32+ return post .UUID , fmt .Errorf ("error making request: %v" , err )
33+ }
34+ return "" , nil
2735 }
2836 defer resp .Body .Close ()
2937
3038 body , err := io .ReadAll (resp .Body )
3139 if err != nil {
32- return post .UUID , fmt .Errorf ("error reading response: %v" , err )
40+ if shouldThrowError {
41+ return post .UUID , fmt .Errorf ("error reading response: %v" , err )
42+ }
43+ return "" , nil
3344 }
3445
3546 if resp .StatusCode == http .StatusNotFound || string (body ) == "player not found" {
47+ if shouldThrowError {
48+ return post .UUID , fmt .Errorf ("invalid username or UUID provided" )
49+ }
50+
3651 return "Player not Found" , nil
3752 }
3853
54+ if len (body ) == 0 {
55+ if shouldThrowError {
56+ return post .UUID , fmt .Errorf ("received empty response from API" )
57+ }
58+ return "" , nil
59+ }
60+
3961 var json = jsoniter .ConfigCompatibleWithStandardLibrary
4062 err = json .Unmarshal (body , & post )
4163 if err != nil {
42- return post .UUID , fmt .Errorf ("error parsing JSON: %v" , err )
64+ if shouldThrowError {
65+ return post .UUID , fmt .Errorf ("error parsing JSON: %v" , err )
66+ }
67+ return "" , nil
4368 }
4469
4570 redis .Set (fmt .Sprintf ("uuid:%s" , strings .ToLower (post .Name )), post .UUID , 24 * 60 * 60 ) // Cache for 24 hours
@@ -48,7 +73,12 @@ func GetUUID(username string) (string, error) {
4873 return post .UUID , nil
4974}
5075
51- func GetUsername (uuid string ) (string , error ) {
76+ func GetUsername (uuid string , throwAnError ... bool ) (string , error ) {
77+ shouldThrowError := true
78+ if len (throwAnError ) > 0 {
79+ shouldThrowError = throwAnError [0 ]
80+ }
81+
5282 var post models.MowojangReponse
5383
5484 cache , err := redis .Get (fmt .Sprintf ("username:%s" , uuid ))
@@ -58,23 +88,39 @@ func GetUsername(uuid string) (string, error) {
5888
5989 resp , err := http .Get (fmt .Sprintf ("https://mowojang.matdoes.dev/%s" , uuid ))
6090 if err != nil {
61- return post .Name , fmt .Errorf ("error making request: %v" , err )
91+ if shouldThrowError {
92+ return post .Name , fmt .Errorf ("error making request: %v" , err )
93+ }
94+ return "" , nil
6295 }
6396 defer resp .Body .Close ()
6497
6598 body , err := io .ReadAll (resp .Body )
6699 if err != nil {
67- return post .Name , fmt .Errorf ("error reading response: %v" , err )
100+ if shouldThrowError {
101+ return post .Name , fmt .Errorf ("error reading response: %v" , err )
102+ }
103+ return "" , nil
68104 }
69105
70106 if resp .StatusCode == http .StatusNotFound || string (body ) == "player not found" {
71107 return "Player not Found" , nil
72108 }
73109
110+ if len (body ) == 0 {
111+ if shouldThrowError {
112+ return post .Name , fmt .Errorf ("received empty response from API" )
113+ }
114+ return "" , nil
115+ }
116+
74117 var json = jsoniter .ConfigCompatibleWithStandardLibrary
75118 err = json .Unmarshal (body , & post )
76119 if err != nil {
77- return post .Name , fmt .Errorf ("error parsing JSON: %v" , err )
120+ if shouldThrowError {
121+ return post .Name , fmt .Errorf ("error parsing JSON: %v" , err )
122+ }
123+ return "" , nil
78124 }
79125
80126 redis .Set (fmt .Sprintf ("uuid:%s" , strings .ToLower (post .Name )), uuid , 24 * 60 * 60 ) // Cache for 24 hours
@@ -83,12 +129,20 @@ func GetUsername(uuid string) (string, error) {
83129 return post .Name , nil
84130}
85131
86- func ResolvePlayer (uuid string ) (* models.MowojangReponse , error ) {
132+ func ResolvePlayer (uuid string , throwAnError ... bool ) (* models.MowojangReponse , error ) {
133+ shouldThrowError := true
134+ if len (throwAnError ) > 0 {
135+ shouldThrowError = throwAnError [0 ]
136+ }
137+
87138 var post models.MowojangReponse
88139 if ! utility .IsUUID (uuid ) {
89- tempUUID , err := GetUUID (uuid )
140+ tempUUID , err := GetUUID (uuid , shouldThrowError )
90141 if err != nil {
91- return & post , fmt .Errorf ("error resolving UUID for username '%s': %v" , uuid , err )
142+ if shouldThrowError {
143+ return & post , fmt .Errorf ("error resolving UUID for username '%s': %v" , uuid , err )
144+ }
145+ return & post , nil
92146 }
93147 uuid = tempUUID
94148 }
@@ -120,6 +174,10 @@ func ResolvePlayer(uuid string) (*models.MowojangReponse, error) {
120174 }, nil
121175 }
122176
177+ if len (body ) == 0 {
178+ return & post , fmt .Errorf ("received empty response from API" )
179+ }
180+
123181 var json = jsoniter .ConfigCompatibleWithStandardLibrary
124182 err = json .Unmarshal (body , & post )
125183 if err != nil {
0 commit comments