focalboard/webapp/src/errors.ts
Scott Bishel d2c4a54d36
GH-2896 - Error on invalid token (#2911)
* handle error on invalid token

* update message and error to be more generic

* fix apostrophe

* send user to origin
2022-04-25 16:11:16 -06:00

107 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {useIntl} from 'react-intl'
import {UserSettings} from './userSettings'
enum ErrorId {
TeamUndefined = 'team-undefined',
NotLoggedIn = 'not-logged-in',
InvalidReadOnlyBoard = 'invalid-read-only-board',
BoardNotFound = 'board-not-found',
}
type ErrorDef = {
title: string
button1Enabled: boolean
button1Text: string
button1Redirect: string | ((params: URLSearchParams) => string)
button1Fill: boolean
button1ClearHistory: boolean
button2Enabled: boolean
button2Text: string
button2Redirect: string | ((params: URLSearchParams) => string)
button2Fill: boolean
button2ClearHistory: boolean
}
function errorDefFromId(id: ErrorId | null): ErrorDef {
const errDef: ErrorDef = {
title: '',
button1Enabled: false,
button1Text: '',
button1Redirect: '',
button1Fill: false,
button1ClearHistory: false,
button2Enabled: false,
button2Text: '',
button2Redirect: '',
button2Fill: false,
button2ClearHistory: false,
}
const intl = useIntl()
switch (id) {
case ErrorId.TeamUndefined: {
errDef.title = intl.formatMessage({id: 'error.team-undefined', defaultMessage: 'Not a valid team.'})
errDef.button1Enabled = true
errDef.button1Text = intl.formatMessage({id: 'error.back-to-home', defaultMessage: 'Back to Home'})
errDef.button1Redirect = (): string => {
UserSettings.setLastTeamID(null)
return window.location.origin
}
errDef.button1Fill = true
break
}
case ErrorId.BoardNotFound: {
errDef.title = intl.formatMessage({id: 'error.board-not-found', defaultMessage: 'Board not found.'})
errDef.button1Enabled = true
errDef.button1Text = intl.formatMessage({id: 'error.back-to-team', defaultMessage: 'Back to team'})
errDef.button1Redirect = '/'
errDef.button1Fill = true
break
}
case ErrorId.NotLoggedIn: {
errDef.title = intl.formatMessage({id: 'error.not-logged-in', defaultMessage: 'Your session may have expired or you\'re not logged in. Log in again to access Boards.'})
errDef.button1Enabled = true
errDef.button1Text = intl.formatMessage({id: 'error.go-login', defaultMessage: 'Login'})
errDef.button1Redirect = '/login'
errDef.button1Redirect = (params: URLSearchParams): string => {
const r = params.get('r')
if (r) {
return `/login?r=${r}`
}
return '/login'
}
errDef.button1Fill = true
break
}
case ErrorId.InvalidReadOnlyBoard: {
errDef.title = intl.formatMessage({id: 'error.invalid-read-only-board', defaultMessage: 'You don\t have access to this board. Log in to access Boards.'})
errDef.button1Enabled = true
errDef.button1Text = intl.formatMessage({id: 'error.go-login', defaultMessage: 'Login'})
errDef.button1Redirect = (): string => {
return window.location.origin
}
errDef.button1Fill = true
break
}
default: {
errDef.title = intl.formatMessage({id: 'error.unknown', defaultMessage: 'An error occurred.'})
errDef.button1Enabled = true
errDef.button1Text = intl.formatMessage({id: 'error.back-to-home', defaultMessage: 'Back to Home'})
errDef.button1Redirect = '/'
errDef.button1Fill = true
errDef.button1ClearHistory = true
break
}
}
return errDef
}
export {ErrorId, ErrorDef, errorDefFromId}