Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force FE redirect to base domain 404 on InstanceNotFoundError #7482

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
included do
set_current_tenant_through_filter
before_action :deduce_and_set_current_tenant

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Layout/TrailingWhitespace: Trailing whitespace detected.

helper_method :current_tenant

rescue_from InstanceNotFoundError, with: :handle_instance_not_found
end

protected

def handle_instance_not_found(exception)
@exception = exception
render json: { error: 'Instance not found' }, status: :not_found

Check warning on line 19 in app/controllers/concerns/application_controller_multitenancy_concern.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/concerns/application_controller_multitenancy_concern.rb#L18-L19

Added lines #L18 - L19 were not covered by tests
end
end
2 changes: 1 addition & 1 deletion app/controllers/concerns/application_multitenancy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
instance = Instance.find_tenant_by_host_or_default(tenant_host)

if Rails.env.production? && instance.default? && instance.host.casecmp(tenant_host) != 0
raise ActionController::RoutingError, 'Instance Not Found'
raise InstanceNotFoundError

Check warning on line 19 in app/controllers/concerns/application_multitenancy.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/concerns/application_multitenancy.rb#L19

Added line #L19 was not covered by tests
end

instance
Expand Down
6 changes: 6 additions & 0 deletions client/app/api/ErrorHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AxiosResponse } from 'axios';

import {
redirectToAuthPage,
redirectToBaseNotFound,
redirectToForbidden,
redirectToNotFound,
} from 'lib/hooks/router/redirect';
Expand All @@ -23,6 +24,10 @@ const isComponentNotFoundResponse = (response?: AxiosResponse): boolean =>
response?.status === 404 &&
response.data?.error?.toLowerCase().includes('component not found'); // NOTE: This string is taken from BE's handle_component_not_found

const isInstanceNotFoundResponse = (response?: AxiosResponse): boolean =>
response?.status === 404 &&
response.data?.error?.toLowerCase().includes('instance not found'); // NOTE: This string is taken from BE's handle_component_not_found

export const redirectIfMatchesErrorIn = (response?: AxiosResponse): void => {
if (isUnauthenticatedResponse(response)) {
localStorage.clear();
Expand All @@ -32,4 +37,5 @@ export const redirectIfMatchesErrorIn = (response?: AxiosResponse): void => {
// Should open a new window and login
redirectToForbidden();
if (isComponentNotFoundResponse(response)) redirectToNotFound();
if (isInstanceNotFoundResponse(response)) redirectToBaseNotFound();
};
7 changes: 7 additions & 0 deletions client/app/lib/hooks/router/redirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export const redirectToNotFound = (): void => {
window.location.href = '/404';
};

export const redirectToBaseNotFound = (): void => {
const url = new URL(window.location.href);
url.host = window.location.hostname.split('.').slice(-2).join('.');
url.pathname = '/404';
window.location.href = url.toString();
};

export const getForbiddenSourceURL = (rawURL: string): string | null => {
const url = new URL(rawURL);
return url.searchParams.get(FORBIDDEN_SOURCE_URL_SEARCH_PARAM);
Expand Down
6 changes: 6 additions & 0 deletions lib/autoload/instance_not_found_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true
class InstanceNotFoundError < StandardError
def initialize(message = nil)
super(message || 'Instance not found')

Check warning on line 4 in lib/autoload/instance_not_found_error.rb

View check run for this annotation

Codecov / codecov/patch

lib/autoload/instance_not_found_error.rb#L4

Added line #L4 was not covered by tests
end
end