mirror of
https://github.com/mastodon/mastodon.git
synced 2026-03-21 18:05:23 -05:00
Rescue JSON::ParserError where already converted (#38244)
This commit is contained in:
parent
f5619fc8bc
commit
f07e84f9de
|
|
@ -60,7 +60,7 @@ class Api::V1::DonationCampaignsController < Api::BaseController
|
|||
return JSON.parse(res.body_with_limit) if res.code == 200
|
||||
end
|
||||
end
|
||||
rescue *Mastodon::HTTP_CONNECTION_ERRORS, Oj::ParseError
|
||||
rescue *Mastodon::HTTP_CONNECTION_ERRORS, JSON::ParserError
|
||||
nil
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -314,7 +314,7 @@ module JsonLdHelper
|
|||
return if compare_id.present? && json['id'] != compare_id
|
||||
|
||||
json
|
||||
rescue Oj::ParseError
|
||||
rescue JSON::ParserError
|
||||
nil
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ class Admin::Metrics::Dimension::SoftwareVersionsDimension < Admin::Metrics::Dim
|
|||
value: version,
|
||||
human_value: version,
|
||||
}
|
||||
rescue Terrapin::CommandNotFoundError, Terrapin::ExitStatusError, Oj::ParseError
|
||||
rescue Terrapin::CommandNotFoundError, Terrapin::ExitStatusError, JSON::ParserError
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ class TranslationService::DeepL < TranslationService
|
|||
provider: 'DeepL.com'
|
||||
)
|
||||
end
|
||||
rescue Oj::ParseError
|
||||
rescue JSON::ParserError
|
||||
raise UnexpectedResponseError
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ class TranslationService::LibreTranslate < TranslationService
|
|||
provider: 'LibreTranslate'
|
||||
)
|
||||
end
|
||||
rescue Oj::ParseError
|
||||
rescue JSON::ParserError
|
||||
raise UnexpectedResponseError
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ class VideoMetadataExtractor
|
|||
@metadata = JSON.parse(ffmpeg_command_output, symbolize_names: true)
|
||||
|
||||
parse_metadata
|
||||
rescue Terrapin::ExitStatusError, Oj::ParseError
|
||||
rescue Terrapin::ExitStatusError, JSON::ParserError
|
||||
@invalid = true
|
||||
rescue Terrapin::CommandNotFoundError
|
||||
raise Paperclip::Errors::CommandNotFoundError, 'Could not run the `ffprobe` command. Please install ffmpeg.' # rubocop:disable I18n/RailsI18n/DecorateString -- This error is not user-facing
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ class Webfinger
|
|||
|
||||
def perform
|
||||
Response.new(@uri, body_from_webfinger)
|
||||
rescue Oj::ParseError
|
||||
rescue JSON::ParserError
|
||||
raise Webfinger::Error, "Invalid JSON in response for #{@uri}"
|
||||
rescue Addressable::URI::InvalidURIError
|
||||
raise Webfinger::Error, "Invalid URI for #{@uri}"
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ class ActivityPub::FetchRemoteActorService < BaseService
|
|||
else
|
||||
body_to_json(prefetched_body, compare_id: uri)
|
||||
end
|
||||
rescue Oj::ParseError
|
||||
rescue JSON::ParserError
|
||||
raise Error, "Error parsing JSON-LD document #{uri}"
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ class ActivityPub::ProcessAccountService < BaseService
|
|||
end
|
||||
|
||||
@account
|
||||
rescue Oj::ParseError
|
||||
rescue JSON::ParserError
|
||||
nil
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ class ActivityPub::ProcessCollectionService < BaseService
|
|||
else
|
||||
process_items [@json]
|
||||
end
|
||||
rescue Oj::ParseError
|
||||
rescue JSON::ParserError
|
||||
nil
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ class SoftwareUpdateCheckService < BaseService
|
|||
Request.new(:get, "#{api_url}?version=#{version}").add_headers('Accept' => 'application/json', 'User-Agent' => 'Mastodon update checker').perform do |res|
|
||||
return JSON.parse(res.body_with_limit) if res.code == 200
|
||||
end
|
||||
rescue *Mastodon::HTTP_CONNECTION_ERRORS, Oj::ParseError
|
||||
rescue *Mastodon::HTTP_CONNECTION_ERRORS, JSON::ParserError
|
||||
nil
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -112,6 +112,11 @@ RSpec.describe JsonLdHelper do
|
|||
expect(fetch_resource_without_id_validation('https://host.test/')).to be_nil
|
||||
end
|
||||
|
||||
it 'returns nil if the body is not parsable' do
|
||||
stub_request(:get, 'https://host.test/').to_return(status: 200, body: 'XXX', headers: { 'Content-Type': 'application/activity+json' })
|
||||
expect(fetch_resource_without_id_validation('https://host.test/')).to be_nil
|
||||
end
|
||||
|
||||
it 'returns hash' do
|
||||
stub_request(:get, 'https://host.test/').to_return(status: 200, body: '{}', headers: { 'Content-Type': 'application/activity+json' })
|
||||
expect(fetch_resource_without_id_validation('https://host.test/')).to eq({})
|
||||
|
|
|
|||
|
|
@ -19,6 +19,19 @@ RSpec.describe TranslationService::DeepL do
|
|||
end
|
||||
|
||||
describe '#translate' do
|
||||
context 'with invalid body response' do
|
||||
before do
|
||||
stub_request(:post, 'https://api.deepl.com/v2/translate')
|
||||
.with(body: 'text=Hasta+la+vista&source_lang=ES&target_lang=en&tag_handling=html')
|
||||
.to_return(body: 'XXX')
|
||||
end
|
||||
|
||||
it 'handles error and re-raises' do
|
||||
expect { service.translate(['Hasta la vista'], 'es', 'en') }
|
||||
.to raise_error(TranslationService::UnexpectedResponseError)
|
||||
end
|
||||
end
|
||||
|
||||
it 'returns translation with specified source language' do
|
||||
stub_request(:post, 'https://api.deepl.com/v2/translate')
|
||||
.with(body: 'text=Hasta+la+vista&source_lang=ES&target_lang=en&tag_handling=html')
|
||||
|
|
|
|||
|
|
@ -29,6 +29,19 @@ RSpec.describe TranslationService::LibreTranslate do
|
|||
end
|
||||
|
||||
describe '#translate' do
|
||||
context 'with invalid body response' do
|
||||
before do
|
||||
stub_request(:post, 'https://libretranslate.example.com/translate')
|
||||
.with(body: '{"q":["Hasta la vista"],"source":"es","target":"en","format":"html","api_key":"my-api-key"}')
|
||||
.to_return(body: 'XXX')
|
||||
end
|
||||
|
||||
it 'handles error and re-raises' do
|
||||
expect { service.translate(['Hasta la vista'], 'es', 'en') }
|
||||
.to raise_error(TranslationService::UnexpectedResponseError)
|
||||
end
|
||||
end
|
||||
|
||||
it 'returns translation with specified source language' do
|
||||
stub_request(:post, 'https://libretranslate.example.com/translate')
|
||||
.with(body: '{"q":["Hasta la vista"],"source":"es","target":"en","format":"html","api_key":"my-api-key"}')
|
||||
|
|
|
|||
|
|
@ -37,6 +37,16 @@ RSpec.describe Webfinger do
|
|||
end
|
||||
end
|
||||
|
||||
context 'when response body is not parsable' do
|
||||
it 'raises an error' do
|
||||
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')
|
||||
.to_return(body: 'XXX', headers: { 'Content-Type': 'application/jrd+json' })
|
||||
|
||||
expect { subject }
|
||||
.to raise_error(Webfinger::Error)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when webfinger fails and host meta is used' do
|
||||
before { stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(status: 404) }
|
||||
|
||||
|
|
|
|||
|
|
@ -52,6 +52,19 @@ RSpec.describe 'Donation campaigns' do
|
|||
end
|
||||
end
|
||||
|
||||
context 'when the donation campaign returns bad response' do
|
||||
before do
|
||||
stub_request(:get, "#{api_url}?platform=web&seed=#{seed}&locale=en").to_return(body: 'Cats & Dogs', status: 200)
|
||||
end
|
||||
|
||||
it 'handles the error and returns http empty' do
|
||||
get '/api/v1/donation_campaigns', headers: headers
|
||||
|
||||
expect(response)
|
||||
.to have_http_status(204)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the donation campaign API returns a campaign' do
|
||||
let(:campaign_json) do
|
||||
{
|
||||
|
|
|
|||
|
|
@ -55,6 +55,16 @@ RSpec.describe SoftwareUpdateCheckService do
|
|||
end
|
||||
end
|
||||
|
||||
context 'when the update server returns invalid response body' do
|
||||
before do
|
||||
stub_request(:get, full_update_check_url).to_return(status: 200, body: 'XXX')
|
||||
end
|
||||
|
||||
it 'handles the error and returns' do
|
||||
expect(subject.call).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the server returns new versions' do
|
||||
let(:server_json) do
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user