From 0e9c1a297aa15570d87da356f2ac908f1fd433a3 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 17 Sep 2016 17:03:36 +0200 Subject: [PATCH] Improved error handling for FollowRemoteService --- .../components/reducers/timelines.jsx | 14 +++++++++----- app/controllers/api_controller.rb | 8 ++++++++ app/services/follow_remote_account_service.rb | 18 ++++++++++++------ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/components/reducers/timelines.jsx b/app/assets/javascripts/components/reducers/timelines.jsx index 79d7d1f7d..462c10733 100644 --- a/app/assets/javascripts/components/reducers/timelines.jsx +++ b/app/assets/javascripts/components/reducers/timelines.jsx @@ -1,8 +1,9 @@ -import { TIMELINE_SET, TIMELINE_UPDATE, TIMELINE_DELETE } from '../actions/timelines'; -import { REBLOG_SUCCESS, FAVOURITE_SUCCESS } from '../actions/interactions'; -import { ACCOUNT_SET_SELF, ACCOUNT_FETCH_SUCCESS } from '../actions/accounts'; -import { STATUS_FETCH_SUCCESS } from '../actions/statuses'; -import Immutable from 'immutable'; +import { TIMELINE_SET, TIMELINE_UPDATE, TIMELINE_DELETE } from '../actions/timelines'; +import { REBLOG_SUCCESS, FAVOURITE_SUCCESS } from '../actions/interactions'; +import { ACCOUNT_SET_SELF, ACCOUNT_FETCH_SUCCESS, ACCOUNT_FOLLOW_SUCCESS, ACCOUNT_UNFOLLOW_SUCCESS } from '../actions/accounts'; +import { STATUS_FETCH_SUCCESS } from '../actions/statuses'; +import { FOLLOW_SUBMIT_SUCCESS } from '../actions/follow'; +import Immutable from 'immutable'; const initialState = Immutable.Map({ home: Immutable.List([]), @@ -97,6 +98,9 @@ export default function timelines(state = initialState, action) { map.set('me', action.account.id); }); case ACCOUNT_FETCH_SUCCESS: + case FOLLOW_SUBMIT_SUCCESS: + case ACCOUNT_FOLLOW_SUCCESS: + case ACCOUNT_UNFOLLOW_SUCCESS: return accountToMaps(state, Immutable.fromJS(action.account)); case STATUS_FETCH_SUCCESS: return contextToMaps(state, Immutable.fromJS(action.status), Immutable.fromJS(action.context.ancestors), Immutable.fromJS(action.context.descendants)); diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index bacdd997b..cbe7141b9 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -10,6 +10,14 @@ class ApiController < ApplicationController render json: { error: 'Record not found' }, status: 404 end + rescue_from Goldfinger::Error do + render json: { error: 'Remote account could not be resolved' }, status: 422 + end + + rescue_from HTTP::Error do + render json: { error: 'Remote data could not be fetched' }, status: 503 + end + protected def current_resource_owner diff --git a/app/services/follow_remote_account_service.rb b/app/services/follow_remote_account_service.rb index 0e23ed7d2..00285f47a 100644 --- a/app/services/follow_remote_account_service.rb +++ b/app/services/follow_remote_account_service.rb @@ -31,9 +31,12 @@ class FollowRemoteAccountService < BaseService feed = get_feed(account.remote_url) hubs = feed.xpath('//xmlns:link[@rel="hub"]') - if hubs.empty? || hubs.first.attribute('href').nil? || feed.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri').nil? - Rails.logger.debug "Cannot find PuSH hub or author for #{uri}" - return nil + if hubs.empty? || hubs.first.attribute('href').nil? + raise Goldfinger::Error, "No PubSubHubbub hubs found" + end + + if feed.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri').nil? + raise Goldfinger::Error, "No author URI found" end account.uri = feed.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri').content @@ -53,9 +56,6 @@ class FollowRemoteAccountService < BaseService end return account - rescue Goldfinger::Error, HTTP::Error - Rails.logger.debug "Error while fetching data for #{uri}" - nil end private @@ -89,3 +89,9 @@ class FollowRemoteAccountService < BaseService HTTP end end + +class NoAuthorFeedError < StandardError +end + +class NoHubError < StandardError +end