Rescue JSON::ParserError where already converted (#38244)
This commit is contained in:
@@ -60,7 +60,7 @@ class Api::V1::DonationCampaignsController < Api::BaseController
|
|||||||
return JSON.parse(res.body_with_limit) if res.code == 200
|
return JSON.parse(res.body_with_limit) if res.code == 200
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue *Mastodon::HTTP_CONNECTION_ERRORS, Oj::ParseError
|
rescue *Mastodon::HTTP_CONNECTION_ERRORS, JSON::ParserError
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -314,7 +314,7 @@ module JsonLdHelper
|
|||||||
return if compare_id.present? && json['id'] != compare_id
|
return if compare_id.present? && json['id'] != compare_id
|
||||||
|
|
||||||
json
|
json
|
||||||
rescue Oj::ParseError
|
rescue JSON::ParserError
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ class Admin::Metrics::Dimension::SoftwareVersionsDimension < Admin::Metrics::Dim
|
|||||||
value: version,
|
value: version,
|
||||||
human_value: version,
|
human_value: version,
|
||||||
}
|
}
|
||||||
rescue Terrapin::CommandNotFoundError, Terrapin::ExitStatusError, Oj::ParseError
|
rescue Terrapin::CommandNotFoundError, Terrapin::ExitStatusError, JSON::ParserError
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ class TranslationService::DeepL < TranslationService
|
|||||||
provider: 'DeepL.com'
|
provider: 'DeepL.com'
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
rescue Oj::ParseError
|
rescue JSON::ParserError
|
||||||
raise UnexpectedResponseError
|
raise UnexpectedResponseError
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ class TranslationService::LibreTranslate < TranslationService
|
|||||||
provider: 'LibreTranslate'
|
provider: 'LibreTranslate'
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
rescue Oj::ParseError
|
rescue JSON::ParserError
|
||||||
raise UnexpectedResponseError
|
raise UnexpectedResponseError
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ class VideoMetadataExtractor
|
|||||||
@metadata = JSON.parse(ffmpeg_command_output, symbolize_names: true)
|
@metadata = JSON.parse(ffmpeg_command_output, symbolize_names: true)
|
||||||
|
|
||||||
parse_metadata
|
parse_metadata
|
||||||
rescue Terrapin::ExitStatusError, Oj::ParseError
|
rescue Terrapin::ExitStatusError, JSON::ParserError
|
||||||
@invalid = true
|
@invalid = true
|
||||||
rescue Terrapin::CommandNotFoundError
|
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
|
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
|
def perform
|
||||||
Response.new(@uri, body_from_webfinger)
|
Response.new(@uri, body_from_webfinger)
|
||||||
rescue Oj::ParseError
|
rescue JSON::ParserError
|
||||||
raise Webfinger::Error, "Invalid JSON in response for #{@uri}"
|
raise Webfinger::Error, "Invalid JSON in response for #{@uri}"
|
||||||
rescue Addressable::URI::InvalidURIError
|
rescue Addressable::URI::InvalidURIError
|
||||||
raise Webfinger::Error, "Invalid URI for #{@uri}"
|
raise Webfinger::Error, "Invalid URI for #{@uri}"
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class ActivityPub::FetchRemoteActorService < BaseService
|
|||||||
else
|
else
|
||||||
body_to_json(prefetched_body, compare_id: uri)
|
body_to_json(prefetched_body, compare_id: uri)
|
||||||
end
|
end
|
||||||
rescue Oj::ParseError
|
rescue JSON::ParserError
|
||||||
raise Error, "Error parsing JSON-LD document #{uri}"
|
raise Error, "Error parsing JSON-LD document #{uri}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ class ActivityPub::ProcessAccountService < BaseService
|
|||||||
end
|
end
|
||||||
|
|
||||||
@account
|
@account
|
||||||
rescue Oj::ParseError
|
rescue JSON::ParserError
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class ActivityPub::ProcessCollectionService < BaseService
|
|||||||
else
|
else
|
||||||
process_items [@json]
|
process_items [@json]
|
||||||
end
|
end
|
||||||
rescue Oj::ParseError
|
rescue JSON::ParserError
|
||||||
nil
|
nil
|
||||||
end
|
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|
|
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
|
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
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -112,6 +112,11 @@ RSpec.describe JsonLdHelper do
|
|||||||
expect(fetch_resource_without_id_validation('https://host.test/')).to be_nil
|
expect(fetch_resource_without_id_validation('https://host.test/')).to be_nil
|
||||||
end
|
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
|
it 'returns hash' do
|
||||||
stub_request(:get, 'https://host.test/').to_return(status: 200, body: '{}', headers: { 'Content-Type': 'application/activity+json' })
|
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({})
|
expect(fetch_resource_without_id_validation('https://host.test/')).to eq({})
|
||||||
|
|||||||
@@ -19,6 +19,19 @@ RSpec.describe TranslationService::DeepL do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '#translate' do
|
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
|
it 'returns translation with specified source language' do
|
||||||
stub_request(:post, 'https://api.deepl.com/v2/translate')
|
stub_request(:post, 'https://api.deepl.com/v2/translate')
|
||||||
.with(body: 'text=Hasta+la+vista&source_lang=ES&target_lang=en&tag_handling=html')
|
.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
|
end
|
||||||
|
|
||||||
describe '#translate' do
|
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
|
it 'returns translation with specified source language' do
|
||||||
stub_request(:post, 'https://libretranslate.example.com/translate')
|
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"}')
|
.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
|
||||||
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
|
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) }
|
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
|
||||||
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
|
context 'when the donation campaign API returns a campaign' do
|
||||||
let(:campaign_json) do
|
let(:campaign_json) do
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -55,6 +55,16 @@ RSpec.describe SoftwareUpdateCheckService do
|
|||||||
end
|
end
|
||||||
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
|
context 'when the server returns new versions' do
|
||||||
let(:server_json) do
|
let(:server_json) do
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user