You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: handle translation API errors instead of panicking - #10
StrTranslate ignored the error from http.PostForm and then deferred resp.Body.Close(), so any network failure caused a nil pointer dereference. It also read translateResp.Data.Translations[0]before checking translateResp.Error.Code, so when the Google API returned an error response (invalid key, quota exceeded, etc.) the handler panicked with an index-out-of-range error and the Error.Message branch could never run.
go vet flags both the first issue (using resp before checking for errors) and a separate format bug in getGitterAvatarUrl, where username (a string) is printed with %d.
Fix
Move the HTTP call into a small googleTranslate helper that returns an error for transport failures, body read failures, invalid JSON, API error responses, and empty translations arrays.
StrTranslate now passes those errors back through TranslateData.ErrMsg, the field the frontend already reads, so it no longer panics.
Use %s for username in the gitter avatar retry log.
Tests
Added object/translator_test.go, which runs googleTranslate against an httptest server for success, API error, empty result, non-JSON response, and unreachable endpoint cases. These tests don't need a database:
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You have signed the CLA already but the status is still pending? Let us recheck it.
Thanks for spotting this! Both panics (nil resp on transport errors and the out-of-range Translations[0] on API errors) are real, and so is the %d format bug in the gitter log.
We've fixed it directly in 769a889 with a smaller change that keeps the logic inside StrTranslate. It also adds a request timeout, and the frontend now shows err_msg (it didn't read that field before, so errors would otherwise just show up as an empty translation). We'd rather not add test files to this repo, so closing this PR. Thanks again!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
StrTranslateignored the error fromhttp.PostFormand then deferredresp.Body.Close(), so any network failure caused a nil pointer dereference. It also readtranslateResp.Data.Translations[0]before checkingtranslateResp.Error.Code, so when the Google API returned an error response (invalid key, quota exceeded, etc.) the handler panicked with an index-out-of-range error and theError.Messagebranch could never run.go vetflags both the first issue (using resp before checking for errors) and a separate format bug ingetGitterAvatarUrl, whereusername(a string) is printed with%d.Fix
googleTranslatehelper that returns an error for transport failures, body read failures, invalid JSON, API error responses, and emptytranslationsarrays.StrTranslatenow passes those errors back throughTranslateData.ErrMsg, the field the frontend already reads, so it no longer panics.%sforusernamein the gitter avatar retry log.Tests
Added
object/translator_test.go, which runsgoogleTranslateagainst anhttptestserver for success, API error, empty result, non-JSON response, and unreachable endpoint cases. These tests don't need a database:go vet ./object/no longer reports either issue.