Skip to content

Commit

Permalink
Fix wrapped errors in messagix module
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizzadar committed Jul 19, 2024
1 parent ef6f441 commit 0534ef6
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion messagix/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *Client) processLogin(resp *http.Response, respBody []byte) error {
var loginResp *types.InstagramLoginResponse
err = json.Unmarshal(respBody, &loginResp)
if err != nil {
return fmt.Errorf("failed to unmarshal instagram login response to *types.InstagramLoginResponse (statusCode=%d): %v", statusCode, err)
return fmt.Errorf("failed to unmarshal instagram login response to *types.InstagramLoginResponse (statusCode=%d): %w", statusCode, err)
}
if loginResp.Status == "fail" {
err = fmt.Errorf("failed to process login request (message=%s, statusText=%s, statusCode=%d)", loginResp.Message, loginResp.Status, statusCode)
Expand Down
2 changes: 1 addition & 1 deletion messagix/byter/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (b *byter) ReadToStruct(s interface{}) error {
jsonData := b.Buff.Next(b.Buff.Len())
err := json.Unmarshal(jsonData, field.Addr().Interface())
if err != nil {
return fmt.Errorf("failed to unmarshal JSON for field %s: %v", values.Type().Field(i).Name, err)
return fmt.Errorf("failed to unmarshal JSON for field %s: %w", values.Type().Field(i).Name, err)
}
}
default:
Expand Down
2 changes: 1 addition & 1 deletion messagix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (c *Client) sendCookieConsent(jsDatr string) error {
})
h.Del("x-csrftoken")
if err != nil {
return fmt.Errorf("failed to marshal *types.InstagramCookiesVariables into bytes: %v", err)
return fmt.Errorf("failed to marshal *types.InstagramCookiesVariables into bytes: %w", err)
}
q := &HttpQuery{
DocID: "3810865872362889",
Expand Down
2 changes: 1 addition & 1 deletion messagix/crypto/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
func EncryptPassword(platform int, pubKeyId int, pubKey, password string) (string, error) {
pubKeyBytes, err := hex.DecodeString(pubKey)
if err != nil {
return "", fmt.Errorf("failed to decode pubKey, must be a hex-encoded string: %v", err)
return "", fmt.Errorf("failed to decode pubKey, must be a hex-encoded string: %w", err)
}

buf := bytes.NewBuffer(nil)
Expand Down
2 changes: 1 addition & 1 deletion messagix/facebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (fb *FacebookMethods) Login(identifier, password string) (*cookies.Cookies,

encryptedPW, err := crypto.EncryptPassword(int(types.Facebook), crypto.FacebookPubKeyId, crypto.FacebookPubKey, password)
if err != nil {
return nil, fmt.Errorf("failed to encrypt password for facebook: %v", err)
return nil, fmt.Errorf("failed to encrypt password for facebook: %w", err)
}

loginForm.Email = identifier
Expand Down
6 changes: 3 additions & 3 deletions messagix/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (c *Client) makeGraphQLRequest(name string, variables interface{}) (*http.R

vBytes, err := json.Marshal(variables)
if err != nil {
return nil, nil, fmt.Errorf("failed to marshal graphql variables to json string: %v", err)
return nil, nil, fmt.Errorf("failed to marshal graphql variables to json string: %w", err)
}

payload := c.NewHttpQuery()
Expand Down Expand Up @@ -93,7 +93,7 @@ func (c *Client) makeLSRequest(variables *graphql.LSPlatformGraphQLLightspeedVar
} else {
c.Logger.Debug().Str("respBody", base64.StdEncoding.EncodeToString(respBody[:4096])).Msg("Errored LS response bytes (truncated)")
}
return nil, fmt.Errorf("failed to unmarshal LSRequest response bytes into LSPlatformGraphQLLightspeedRequestQuery struct: %v", err)
return nil, fmt.Errorf("failed to unmarshal LSRequest response bytes into LSPlatformGraphQLLightspeedRequestQuery struct: %w", err)
}
if graphQLData.ErrorCode != 0 {
c.Logger.Warn().
Expand Down Expand Up @@ -124,7 +124,7 @@ func (c *Client) makeLSRequest(variables *graphql.LSPlatformGraphQLLightspeedVar
err = json.Unmarshal(lightSpeedRes, &lsData)
if err != nil {
c.Logger.Debug().RawJSON("respBody", respBody).Msg("Response data for errored inner response")
return nil, fmt.Errorf("failed to unmarshal LSRequest lightspeed payload into lightspeed.LightSpeedData: %v", err)
return nil, fmt.Errorf("failed to unmarshal LSRequest lightspeed payload into lightspeed.LightSpeedData: %w", err)
}

lsTable := &table.LSTable{}
Expand Down
4 changes: 2 additions & 2 deletions messagix/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (c *Client) makeRequestDirect(url string, method string, headers http.Heade
}
}()
if err != nil {
c.UpdateProxy(fmt.Sprintf("http request error: %v", err.Error()))
c.UpdateProxy(fmt.Sprintf("http request error: %w", err.Error()))

Check failure on line 193 in messagix/http.go

View workflow job for this annotation

GitHub Actions / Lint (old)

fmt.Sprintf format %w has arg err.Error() of wrong type string

Check failure on line 193 in messagix/http.go

View workflow job for this annotation

GitHub Actions / Lint (latest)

fmt.Sprintf format %w has arg err.Error() of wrong type string

Check failure on line 193 in messagix/http.go

View workflow job for this annotation

GitHub Actions / Lint (old)

fmt.Sprintf format %w has arg err.Error() of wrong type string

Check failure on line 193 in messagix/http.go

View workflow job for this annotation

GitHub Actions / Lint (latest)

fmt.Sprintf format %w has arg err.Error() of wrong type string
return nil, nil, fmt.Errorf("%w: %w", ErrRequestFailed, err)
}

Expand Down Expand Up @@ -272,7 +272,7 @@ func (c *Client) sendLoginRequest(form url.Values, loginUrl string) (*http.Respo

resp, respBody, err := c.MakeRequest(loginUrl, "POST", h, loginPayload, types.FORM)
if err != nil {
return nil, nil, fmt.Errorf("failed to send login request: %v", err)
return nil, nil, fmt.Errorf("failed to send login request: %w", err)
}

return resp, respBody, nil
Expand Down
22 changes: 11 additions & 11 deletions messagix/instagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (ig *InstagramMethods) Login(identifier, password string) (*cookies.Cookies
login_page_v1 := ig.client.getEndpoint("web_login_page_v1")
_, _, err := ig.client.MakeRequest(login_page_v1, "GET", h, nil, types.NONE)
if err != nil {
return nil, fmt.Errorf("failed to fetch %s for instagram login: %v", login_page_v1, err)
return nil, fmt.Errorf("failed to fetch %s for instagram login: %w", login_page_v1, err)
}

err = ig.client.sendCookieConsent("")
Expand All @@ -48,25 +48,25 @@ func (ig *InstagramMethods) Login(identifier, password string) (*cookies.Cookies
web_shared_data_v1 := ig.client.getEndpoint("web_shared_data_v1")
req, respBody, err := ig.client.MakeRequest(web_shared_data_v1, "GET", h, nil, types.NONE) // returns actual machineId you're supposed to use
if err != nil {
return nil, fmt.Errorf("failed to fetch %s for instagram login: %v", web_shared_data_v1, err)
return nil, fmt.Errorf("failed to fetch %s for instagram login: %w", web_shared_data_v1, err)
}

ig.client.cookies.UpdateFromResponse(req)

err = json.Unmarshal(respBody, &ig.client.configs.browserConfigTable.XIGSharedData.ConfigData)
if err != nil {
return nil, fmt.Errorf("failed to marshal web_shared_data_v1 resp body into *XIGSharedData.ConfigData: %v", err)
return nil, fmt.Errorf("failed to marshal web_shared_data_v1 resp body into *XIGSharedData.ConfigData: %w", err)
}

encryptionConfig := ig.client.configs.browserConfigTable.XIGSharedData.ConfigData.Encryption
pubKeyId, err := strconv.Atoi(encryptionConfig.KeyID)
if err != nil {
return nil, fmt.Errorf("failed to convert keyId for instagram password encryption to int: %v", err)
return nil, fmt.Errorf("failed to convert keyId for instagram password encryption to int: %w", err)
}

encryptedPw, err := crypto.EncryptPassword(int(types.Instagram), pubKeyId, encryptionConfig.PublicKey, password)
if err != nil {
return nil, fmt.Errorf("failed to encrypt password for instagram: %v", err)
return nil, fmt.Errorf("failed to encrypt password for instagram: %w", err)
}

loginForm := &types.InstagramLoginPayload{
Expand Down Expand Up @@ -100,15 +100,15 @@ func (ig *InstagramMethods) FetchProfile(username string) (*responses.ProfileInf

resp, respBody, err := ig.client.MakeRequest(reqUrl, "GET", h, nil, types.NONE)
if err != nil {
return nil, fmt.Errorf("failed to fetch the profile by username @%s: %v", username, err)
return nil, fmt.Errorf("failed to fetch the profile by username @%s: %w", username, err)
}

ig.client.cookies.UpdateFromResponse(resp)

var profileInfo *responses.ProfileInfoResponse
err = json.Unmarshal(respBody, &profileInfo)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response bytes into *responses.ProfileInfoResponse (statusCode=%d): %v", resp.StatusCode, err)
return nil, fmt.Errorf("failed to unmarshal response bytes into *responses.ProfileInfoResponse (statusCode=%d): %w", resp.StatusCode, err)
}

return profileInfo, nil
Expand All @@ -127,15 +127,15 @@ func (ig *InstagramMethods) FetchMedia(mediaID, mediaShortcode string) (*respons

resp, respBody, err := ig.client.MakeRequest(reqUrl, "GET", h, nil, types.NONE)
if err != nil {
return nil, fmt.Errorf("failed to fetch the media by id %s: %v", mediaID, err)
return nil, fmt.Errorf("failed to fetch the media by id %s: %w", mediaID, err)
}

ig.client.cookies.UpdateFromResponse(resp)

var mediaInfo *responses.FetchMediaResponse
err = json.Unmarshal(respBody, &mediaInfo)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response bytes into *responses.FetchMediaResponse (statusCode=%d): %v", resp.StatusCode, err)
return nil, fmt.Errorf("failed to unmarshal response bytes into *responses.FetchMediaResponse (statusCode=%d): %w", resp.StatusCode, err)
}

return mediaInfo, nil
Expand All @@ -157,15 +157,15 @@ func (ig *InstagramMethods) FetchReel(reelIds []string, mediaID string) (*respon
reqUrl := ig.client.getEndpoint("reels_media") + query.Encode()
resp, respBody, err := ig.client.MakeRequest(reqUrl, "GET", h, nil, types.NONE)
if err != nil {
return nil, fmt.Errorf("failed to fetch reels by ids %v: %v", reelIds, err)
return nil, fmt.Errorf("failed to fetch reels by ids %v: %w", reelIds, err)
}

ig.client.cookies.UpdateFromResponse(resp)

var reelInfo *responses.ReelInfoResponse
err = json.Unmarshal(respBody, &reelInfo)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response bytes into *responses.ReelInfoResponse (statusCode=%d): %v", resp.StatusCode, err)
return nil, fmt.Errorf("failed to unmarshal response bytes into *responses.ReelInfoResponse (statusCode=%d): %w", resp.StatusCode, err)
}

return reelInfo, nil
Expand Down
2 changes: 1 addition & 1 deletion messagix/js_module_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (m *ModuleParser) Load(page string) error {
case "script":
doneCrawling, err = m.crawlJavascriptFile(href)
if err != nil {
return fmt.Errorf("messagix-moduleparser: failed to crawl js file %s (%v)", href, err)
return fmt.Errorf("messagix-moduleparser: failed to crawl js file %s (%w)", href, err)
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions messagix/mercury.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *Client) SendMercuryUploadRequest(ctx context.Context, threadID int64, m
urlQueries := c.NewHttpQuery()
queryValues, err := query.Values(urlQueries)
if err != nil {
return nil, fmt.Errorf("failed to convert HttpQuery into query.Values for mercury upload: %v", err)
return nil, fmt.Errorf("failed to convert HttpQuery into query.Values for mercury upload: %w", err)
}

payloadQuery := queryValues.Encode()
Expand All @@ -53,7 +53,7 @@ func (c *Client) SendMercuryUploadRequest(ctx context.Context, threadID int64, m

_, respBody, err := c.MakeRequest(url, "POST", h, payload, types.NONE)
if err != nil {
return nil, fmt.Errorf("failed to send MercuryUploadRequest: %v", err)
return nil, fmt.Errorf("failed to send MercuryUploadRequest: %w", err)
}

resp, err := c.parseMercuryResponse(ctx, respBody)
Expand All @@ -77,7 +77,7 @@ func (c *Client) parseMercuryResponse(ctx context.Context, respBody []byte) (*ty

var mercuryResponse *types.MercuryUploadResponse
if err := json.Unmarshal(jsonData, &mercuryResponse); err != nil {
return nil, fmt.Errorf("failed to parse mercury response: %v", err)
return nil, fmt.Errorf("failed to parse mercury response: %w", err)
} else if mercuryResponse.ErrorCode != 0 {
return nil, fmt.Errorf("error in mercury upload: %w", &mercuryResponse.ErrorResponse)
}
Expand All @@ -102,14 +102,14 @@ func (c *Client) parseMetadata(response *types.MercuryUploadResponse) error {
var realMetadata []types.FileMetadata
err := json.Unmarshal(response.Payload.Metadata, &realMetadata)
if err != nil {
return fmt.Errorf("failed to unmarshal image metadata in upload response: %v", err)
return fmt.Errorf("failed to unmarshal image metadata in upload response: %w", err)
}
response.Payload.RealMetadata = &realMetadata[0]
case '{':
var realMetadata map[string]types.FileMetadata
err := json.Unmarshal(response.Payload.Metadata, &realMetadata)
if err != nil {
return fmt.Errorf("failed to unmarshal video metadata in upload response: %v", err)
return fmt.Errorf("failed to unmarshal video metadata in upload response: %w", err)
}
realMetaEntry := realMetadata["0"]
response.Payload.RealMetadata = &realMetaEntry
Expand All @@ -127,24 +127,24 @@ func (c *Client) NewMercuryMediaPayload(media *MercuryUploadMedia) ([]byte, stri

err := writer.SetBoundary("----WebKitFormBoundary" + methods.RandStr(16))
if err != nil {
return nil, "", fmt.Errorf("messagix-mercury: Failed to set boundary (%v)", err)
return nil, "", fmt.Errorf("messagix-mercury: Failed to set boundary (%w)", err)
}

if media.IsVoiceClip {
err = writer.WriteField("voice_clip", "true")
if err != nil {
return nil, "", fmt.Errorf("messagix-mercury: Failed to write voice_clip field (%v)", err)
return nil, "", fmt.Errorf("messagix-mercury: Failed to write voice_clip field (%w)", err)
}

if media.WaveformData != nil {
waveformBytes, err := json.Marshal(media.WaveformData)
if err != nil {
return nil, "", fmt.Errorf("messagix-mercury: Failed to marshal waveform (%v)", err)
return nil, "", fmt.Errorf("messagix-mercury: Failed to marshal waveform (%w)", err)
}

err = writer.WriteField("voice_clip_waveform_data", string(waveformBytes))
if err != nil {
return nil, "", fmt.Errorf("messagix-mercury: Failed to write waveform field (%v)", err)
return nil, "", fmt.Errorf("messagix-mercury: Failed to write waveform field (%w)", err)
}
}
}
Expand All @@ -155,17 +155,17 @@ func (c *Client) NewMercuryMediaPayload(media *MercuryUploadMedia) ([]byte, stri

mediaPart, err := writer.CreatePart(partHeader)
if err != nil {
return nil, "", fmt.Errorf("messagix-mercury: Failed to create multipart writer (%v)", err)
return nil, "", fmt.Errorf("messagix-mercury: Failed to create multipart writer (%w)", err)
}

_, err = mediaPart.Write(media.MediaData)
if err != nil {
return nil, "", fmt.Errorf("messagix-mercury: Failed to write data to multipart section (%v)", err)
return nil, "", fmt.Errorf("messagix-mercury: Failed to write data to multipart section (%w)", err)
}

err = writer.Close()
if err != nil {
return nil, "", fmt.Errorf("messagix-mercury: Failed to close multipart writer (%v)", err)
return nil, "", fmt.Errorf("messagix-mercury: Failed to close multipart writer (%w)", err)
}

return mercuryPayload.Bytes(), writer.FormDataContentType(), nil
Expand Down
10 changes: 5 additions & 5 deletions messagix/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ func (m *ModuleParser) HandleRawJSON(data []byte, id string) error {
var d types.EnvJSON
err = json.Unmarshal(data, &d)
if err != nil {
return fmt.Errorf("failed to parse data from types.EnvJSON: %v", err)
return fmt.Errorf("failed to parse data from types.EnvJSON: %w", err)
}
case "__eqmc":
var d types.Eqmc
err = json.Unmarshal(data, &d)
if err != nil {
return fmt.Errorf("failed to parse data from types.Eqmc: %v", err)
return fmt.Errorf("failed to parse data from types.Eqmc: %w", err)
}
ajaxData, err := d.ParseAjaxURLData()
if err != nil {
return fmt.Errorf("failed to parse ajax url data from types.Eqmc: %v", err)
return fmt.Errorf("failed to parse ajax url data from types.Eqmc: %w", err)
}
m.client.configs.Jazoest = ajaxData.Jazoest
m.client.configs.CometReq = ajaxData.CometReq
Expand Down Expand Up @@ -208,7 +208,7 @@ func (m *ModuleParser) handleLightSpeedQLRequest(data json.RawMessage, parserFun
var payload lightspeed.LightSpeedData
err := json.Unmarshal([]byte(lsPayloadStr), &payload)
if err != nil {
return fmt.Errorf("messagix-moduleparser: failed to marshal lsPayloadStr into LightSpeedData: %v", err)
return fmt.Errorf("messagix-moduleparser: failed to marshal lsPayloadStr into LightSpeedData: %w", err)
}

decoder := lightspeed.NewLightSpeedDecoder(deps.ToMap(), m.LS)
Expand Down Expand Up @@ -335,7 +335,7 @@ func (m *ModuleParser) parseCSRBit(s string) ([]int, error) {
for _, b := range splitUp {
conv, err := strconv.ParseInt(b, 10, 32)
if err != nil {
return nil, fmt.Errorf("messagix-moduleparser: failed to parse csrbit: %v", err)
return nil, fmt.Errorf("messagix-moduleparser: failed to parse csrbit: %w", err)
}
if conv == 0 {
continue
Expand Down
4 changes: 2 additions & 2 deletions messagix/syncManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (sm *SyncManager) SyncSocketData(databaseId int64, db *socket.QueryMetadata

jsonPayload, err := json.Marshal(&payload)
if err != nil {
return nil, fmt.Errorf("failed to marshal DatabaseQuery struct into json bytes (databaseId=%d): %v", databaseId, err)
return nil, fmt.Errorf("failed to marshal DatabaseQuery struct into json bytes (databaseId=%d): %w", databaseId, err)
}

sm.client.Logger.Trace().
Expand All @@ -110,7 +110,7 @@ func (sm *SyncManager) SyncSocketData(databaseId int64, db *socket.QueryMetadata
Msg("Syncing database via socket")
resp, err := sm.client.socket.makeLSRequest(jsonPayload, t)
if err != nil {
return nil, fmt.Errorf("failed to make lightspeed socket request with DatabaseQuery byte payload (databaseId=%d): %v", databaseId, err)
return nil, fmt.Errorf("failed to make lightspeed socket request with DatabaseQuery byte payload (databaseId=%d): %w", databaseId, err)
}

resp.Finish()
Expand Down
2 changes: 1 addition & 1 deletion messagix/threads.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (c *Client) ExecuteTasks(tasks ...socket.Task) (*table.LSTable, error) {

payload, err := tskm.FinalizePayload()
if err != nil {
return nil, fmt.Errorf("failed to finalize payload: %v", err)
return nil, fmt.Errorf("failed to finalize payload: %w", err)
}

resp, err := c.socket.makeLSRequest(payload, 3)
Expand Down

0 comments on commit 0534ef6

Please sign in to comment.