@@ -3,6 +3,7 @@ package aspell
33import (
44 "encoding/json"
55 "fmt"
6+ "log"
67 "net/http"
78 "os"
89 "strings"
@@ -14,26 +15,39 @@ func fetchRemoteFile(aspell Aspell) ([]string, error) {
1415 url := aspell .RemoteFile .URL
1516 if aspell .RemoteFile .URLEnv != "" {
1617 url = os .Getenv (aspell .RemoteFile .URLEnv )
18+ log .Printf ("aspell remote file: using URL from env %s: %q" , aspell .RemoteFile .URLEnv , url )
19+ } else {
20+ log .Printf ("aspell remote file: using URL: %q" , url )
21+ }
22+
23+ if url == "" {
24+ return nil , fmt .Errorf ("aspell remote file: URL is empty" )
1725 }
1826
1927 req , err := http .NewRequest ("GET" , url , nil )
2028 if err != nil {
21- return nil , err
29+ return nil , fmt . Errorf ( "aspell remote file: failed to build request: %w" , err )
2230 }
2331
2432 if aspell .RemoteFile .HeaderFromENV != "" {
2533 envValue := os .Getenv (aspell .RemoteFile .HeaderFromENV )
34+ if envValue == "" {
35+ log .Printf ("aspell remote file: warning: header env %s is empty" , aspell .RemoteFile .HeaderFromENV )
36+ }
2637 req .Header .Set (aspell .RemoteFile .HeaderFromENV , envValue )
2738 }
2839 if aspell .RemoteFile .PrivateTokenENV != "" {
2940 envValue := os .Getenv (aspell .RemoteFile .PrivateTokenENV )
41+ if envValue == "" {
42+ log .Printf ("aspell remote file: warning: private token env %s is empty" , aspell .RemoteFile .PrivateTokenENV )
43+ }
3044 req .Header .Set ("PRIVATE-TOKEN" , envValue )
3145 }
3246
3347 client := & http.Client {}
3448 resp , err := client .Do (req )
3549 if err != nil {
36- return nil , err
50+ return nil , fmt . Errorf ( "aspell remote file: request failed: %w" , err )
3751 }
3852 defer resp .Body .Close ()
3953
@@ -44,7 +58,7 @@ func fetchRemoteFile(aspell Aspell) ([]string, error) {
4458 var data map [string ]interface {}
4559 err = json .NewDecoder (resp .Body ).Decode (& data )
4660 if err != nil {
47- return nil , err
61+ return nil , fmt . Errorf ( "aspell remote file: failed to decode JSON: %w" , err )
4862 }
4963
5064 var allowedWords []string
@@ -53,24 +67,34 @@ func fetchRemoteFile(aspell Aspell) ([]string, error) {
5367 if ! ok {
5468 content , ok := data [aspell .RemoteFile .AllowedItemsKey ].(string )
5569 if ! ok {
56- return nil , nil
70+ return nil , fmt . Errorf ( "aspell remote file: key %q not found or not a string/array in response" , aspell . RemoteFile . AllowedItemsKey )
5771 }
72+ content = strings .TrimRight (content , "\n " )
5873 if strings .HasPrefix (content , "```yaml\n " ) && strings .HasSuffix (content , "\n ```" ) {
5974 content = strings .TrimPrefix (content , "```yaml\n " )
6075 content = strings .TrimSuffix (content , "\n ```" )
6176 err = yaml .Unmarshal ([]byte (content ), & allowedWords )
6277 if err != nil {
63- return nil , err
78+ return nil , fmt . Errorf ( "aspell remote file: failed to parse YAML block: %w" , err )
6479 }
65-
80+ log . Printf ( "aspell remote file: loaded %d words (yaml block): %v" , len ( allowedWords ), wordSample ( allowedWords ))
6681 return allowedWords , nil
6782 }
6883 allowedWords = strings .Split (content , "\n " )
69- }
70-
71- for _ , item := range items {
72- allowedWords = append (allowedWords , item .(string ))
84+ log .Printf ("aspell remote file: loaded %d words (newline-separated): %v" , len (allowedWords ), wordSample (allowedWords ))
85+ } else {
86+ for _ , item := range items {
87+ allowedWords = append (allowedWords , item .(string ))
88+ }
89+ log .Printf ("aspell remote file: loaded %d words (JSON array): %v" , len (allowedWords ), wordSample (allowedWords ))
7390 }
7491
7592 return allowedWords , nil
7693}
94+
95+ func wordSample (words []string ) []string {
96+ if len (words ) <= 3 {
97+ return words
98+ }
99+ return []string {words [0 ], words [1 ], "..." , words [len (words )- 1 ]}
100+ }
0 commit comments