Skip to content

Commit

Permalink
remove ERROR_CODE
Browse files Browse the repository at this point in the history
  • Loading branch information
peteranny committed Dec 8, 2024
1 parent 5ee6429 commit d614937
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 89 deletions.
59 changes: 28 additions & 31 deletions src/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ import { queryMeApi } from 'apis/me';
import authStatus from 'constants/authStatus';
import { pushErrorNotificationAndRollbarAndThrowError } from 'actions/toastNotification';
import { GraphqlError } from 'utils/errors';
import * as ERROR_CODE from 'constants/errorCode';
import {
ER0001,
ER0002,
ER0003,
ER0004,
ER0006,
ER0009,
ER0010,
ER0011,
ER0012,
ER0013,
ER0014,
ER0015,
ER0016,
} from 'constants/errorCodeMsg';

export const SET_LOGIN = '@@auth/SET_LOGIN';
export const SET_USER = '@@auth/SET_USER';
Expand Down Expand Up @@ -45,29 +59,27 @@ const FBSDKLogin = FB => {
*/
export const loginWithFB = FBSDK => async (dispatch, getState) => {
if (!FBSDK) {
dispatch(pushErrorNotificationAndRollbarAndThrowError(ERROR_CODE.ER0001));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0001));
}

let fbLoginResponse = null;
try {
// invoke FB SDK Login to get FB-issued access token
fbLoginResponse = await FBSDKLogin(FBSDK);
} catch (error) {
dispatch(
pushErrorNotificationAndRollbarAndThrowError(ERROR_CODE.ER0002, error),
);
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0002, error));
}

if (!fbLoginResponse || !fbLoginResponse.status) {
dispatch(pushErrorNotificationAndRollbarAndThrowError(ERROR_CODE.ER0003));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0003));
}

switch (fbLoginResponse.status) {
case authStatus.CANCELED:
return;
case authStatus.NOT_AUTHORIZED:
dispatch(setLogin(authStatus.NOT_AUTHORIZED));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ERROR_CODE.ER0004));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0004));
break;
case authStatus.CONNECTED:
try {
Expand All @@ -79,29 +91,20 @@ export const loginWithFB = FBSDK => async (dispatch, getState) => {
} catch (error) {
if (error instanceof GraphqlError && error.codes) {
if (error.codes[0] === 'UNAUTHENTICATED') {
dispatch(
pushErrorNotificationAndRollbarAndThrowError(ERROR_CODE.ER0014),
);
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0014));
break;
} else if (error.codes[0] === 'FORBIDDEN') {
dispatch(
pushErrorNotificationAndRollbarAndThrowError(ERROR_CODE.ER0015),
);
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0015));
break;
}
}
dispatch(
pushErrorNotificationAndRollbarAndThrowError(
ERROR_CODE.ER0016,
error,
),
);
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0016, error));
}
break;
default:
dispatch(
pushErrorNotificationAndRollbarAndThrowError(
ERROR_CODE.ER0006,
ER0006,
null,
fbLoginResponse,
),
Expand All @@ -122,33 +125,27 @@ export const loginWithGoogle = credentialResponse => async (
) => {
// TODO: 當登入失敗
if (!credentialResponse || !credentialResponse.credential) {
dispatch(pushErrorNotificationAndRollbarAndThrowError(ERROR_CODE.ER0009));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0009));
}
const idToken = credentialResponse.credential;
try {
const response = await postAuthGoogleApi({ idToken });
if (response && response.token) {
await dispatch(loginWithToken(response.token));
} else {
dispatch(pushErrorNotificationAndRollbarAndThrowError(ERROR_CODE.ER0010));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0010));
}
} catch (error) {
if (error instanceof GraphqlError && error.codes) {
if (error.codes[0] === 'UNAUTHENTICATED') {
dispatch(
pushErrorNotificationAndRollbarAndThrowError(ERROR_CODE.ER0011),
);
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0011));
return;
} else if (error.codes[0] === 'FORBIDDEN') {
dispatch(
pushErrorNotificationAndRollbarAndThrowError(ERROR_CODE.ER0012),
);
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0012));
return;
}
}
dispatch(
pushErrorNotificationAndRollbarAndThrowError(ERROR_CODE.ER0013, error),
);
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0013, error));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import Header, { CompanyJobTitleHeader } from '../../common/TypeFormHeader';
import SubmittableFormBuilder from '../../common/SubmittableFormBuilder';
import { createInterviewExperience } from 'actions/experiences';
import { GA_CATEGORY, GA_ACTION } from 'constants/gaConstants';
import * as ERROR_CODE from 'constants/errorCode';
import { ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import { ER0008, ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import {
DATA_KEY_COMPANY_NAME,
DATA_KEY_JOB_TITLE,
Expand Down Expand Up @@ -166,7 +165,7 @@ const TypeForm = ({ open, onClose }) => {
category: GA_CATEGORY.SHARE_INTERVIEW_TYPE_FORM,
action: GA_ACTION.UPLOAD_FAIL,
});
const errorCode = ERROR_CODE.ER0008;
const errorCode = ER0008;
rollbar.error(
`[${errorCode}] ${ERROR_CODE_MSG[errorCode].internal} ${error.message}`,
error,
Expand Down
5 changes: 2 additions & 3 deletions src/components/ShareExperience/TimeSalaryForm/TypeForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
DATA_KEY_HAS_OVERTIME_SALARY,
DATA_KEY_HAS_COMPENSATORY_DAYOFF,
} from '../constants';
import { ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import { ER0007, ERROR_CODE_MSG } from 'constants/errorCodeMsg';

import { evolve } from '../utils';
import { generateTabURL, pageType, tabType } from 'constants/companyJobTitle';
Expand All @@ -53,7 +53,6 @@ import { getUserPseudoId } from 'utils/GAUtils';
import rollbar from 'utils/rollbar';

import { GA_MEASUREMENT_ID } from '../../../config';
import * as ERROR_CODE from 'constants/errorCode';

const header = <Header title="請輸入你的一份薪資工時" />;

Expand Down Expand Up @@ -168,7 +167,7 @@ const TypeForm = ({ open, onClose, hideProgressBar = false }) => {
: GA_CATEGORY.SHARE_TIME_SALARY_TYPE_FORM,
action: GA_ACTION.UPLOAD_FAIL,
});
const errorCode = ERROR_CODE.ER0007;
const errorCode = ER0007;
rollbar.error(
`[${errorCode}] ${ERROR_CODE_MSG[errorCode].internal} ${error.message}`,
error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ import { tabType } from '../../../constants/companyJobTitle';
import { createWorkExperienceWithRating } from 'actions/experiences';
import { transferKeyToSnakecase } from 'utils/objectUtil';
import { GA_CATEGORY, GA_ACTION } from 'constants/gaConstants';
import * as ERROR_CODE from 'constants/errorCode';
import { ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import { ER0020, ERROR_CODE_MSG } from 'constants/errorCodeMsg';

import { sendEvent } from 'utils/hotjarUtil';
import rollbar from 'utils/rollbar';
Expand Down Expand Up @@ -140,7 +139,7 @@ const TypeForm = ({ open, onClose, hideProgressBar = false }) => {
category: GA_CATEGORY.SHARE_WORK,
action: GA_ACTION.UPLOAD_FAIL,
});
const errorCode = ERROR_CODE.ER0020;
const errorCode = ER0020;
rollbar.error(
`[${errorCode}] ${ERROR_CODE_MSG[errorCode].internal} ${error.message}`,
error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import ConfirmModal from 'common/FormBuilder/Modals/ConfirmModal';
import Footer from './TypeFormFooter';
import { useExperienceCount, useSalaryWorkTimeCount } from 'hooks/useCount';
import rollbar from 'utils/rollbar';
import * as ERROR_CODE from 'constants/errorCode';
import { ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import { ER0018, ERROR_CODE_MSG } from 'constants/errorCodeMsg';

const SubmittableTypeForm = ({
open,
Expand Down Expand Up @@ -39,7 +38,7 @@ const SubmittableTypeForm = ({
setSubmittedDraft(draft);
setSubmitStatus('success');
} catch (error) {
const errorCode = ERROR_CODE.ER0018;
const errorCode = ER0018;
rollbar.error(
`[${errorCode}] ${ERROR_CODE_MSG[errorCode].internal} ${error}`,
error,
Expand Down
7 changes: 3 additions & 4 deletions src/components/common/FormBuilder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import AnimatedPager from './AnimatedPager';
import styles from './FormBuilder.module.css';
import { OptionPropType } from './QuestionBuilder/Checkbox/PropTypes';
import rollbar from 'utils/rollbar';
import * as ERROR_CODE from 'constants/errorCode';
import { ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import { ER0019, ER0021, ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import { useDispatch } from 'react-redux';
import { pushErrorNotificationAndRollbar } from 'actions/toastNotification';

Expand Down Expand Up @@ -124,11 +123,11 @@ const FormBuilder = ({
if (warning) {
if (onValidateFail)
onValidateFail({ dataKey, value: draft[dataKey], warning });
else dispatch(pushErrorNotificationAndRollbar(ERROR_CODE.ER0021));
else dispatch(pushErrorNotificationAndRollbar(ER0021));
} else if (isSubmittable) {
onSubmit(draft);
} else {
const errorCode = ERROR_CODE.ER0019;
const errorCode = ER0019;
rollbar.error(
`[${errorCode}] ${ERROR_CODE_MSG[errorCode].internal}`,
null,
Expand Down
20 changes: 0 additions & 20 deletions src/constants/errorCode.js

This file was deleted.

63 changes: 41 additions & 22 deletions src/constants/errorCodeMsg.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,108 @@
import * as ERROR_CODE from './errorCode';

const LOGIN_ERROR_MSG =
'登入時發生錯誤,請再重試一次。若錯誤持續發生,請聯繫 [email protected]';

const LOGIN_ERROR_MSG_CLEAN_BROWSER_DATA =
'登入時發生錯誤,請清除瀏覽器資料,再重試一次。若錯誤持續發生,請聯繫 [email protected]';

export const ER0001 = 'ER0001';
export const ER0002 = 'ER0002';
export const ER0003 = 'ER0003';
export const ER0004 = 'ER0004';
export const ER0005 = 'ER0005';
export const ER0006 = 'ER0006';
export const ER0007 = 'ER0007';
export const ER0008 = 'ER0008';
export const ER0009 = 'ER0009';
export const ER0010 = 'ER0010';
export const ER0011 = 'ER0011';
export const ER0012 = 'ER0012';
export const ER0013 = 'ER0013';
export const ER0014 = 'ER0014';
export const ER0015 = 'ER0015';
export const ER0016 = 'ER0016';
export const ER0018 = 'ER0018';
export const ER0019 = 'ER0019';
export const ER0020 = 'ER0020';
export const ER0021 = 'ER0021';

// maintain a list of global error code
export const ERROR_CODE_MSG = {
[ERROR_CODE.ER0001]: {
[ER0001]: {
external: LOGIN_ERROR_MSG,
internal: 'FB SDK is not ready',
},
[ERROR_CODE.ER0002]: {
[ER0002]: {
external: LOGIN_ERROR_MSG,
internal: 'FB SDK login failed',
},
[ERROR_CODE.ER0003]: {
[ER0003]: {
external: LOGIN_ERROR_MSG,
internal: 'FB login response is empty or does not have status field',
},
[ERROR_CODE.ER0004]: {
[ER0004]: {
external: LOGIN_ERROR_MSG,
internal: 'FB login failed: unauthorized',
},
[ERROR_CODE.ER0005]: {
[ER0005]: {
external: LOGIN_ERROR_MSG,
internal: 'Graphql mutation facebookLogin failed',
},
[ERROR_CODE.ER0006]: {
[ER0006]: {
external: LOGIN_ERROR_MSG_CLEAN_BROWSER_DATA,
internal: 'FB login failed: unknown auth status',
},
[ERROR_CODE.ER0007]: {
[ER0007]: {
internal: 'Submit salary failed',
},
[ERROR_CODE.ER0008]: {
[ER0008]: {
internal: 'Submit interview failed',
},
[ERROR_CODE.ER0009]: {
[ER0009]: {
external: LOGIN_ERROR_MSG,
internal: 'Cannot get id_token from Google auth API',
},
[ERROR_CODE.ER0010]: {
[ER0010]: {
external: LOGIN_ERROR_MSG,
internal: 'Cannot get token from Graphql googleLogin mutation response',
},
[ERROR_CODE.ER0011]: {
[ER0011]: {
external: LOGIN_ERROR_MSG,
internal:
'Graphql googleLogin mutation API failed with codes=UNAUTHENTICATED',
},
[ERROR_CODE.ER0012]: {
[ER0012]: {
external: LOGIN_ERROR_MSG,
internal:
'Graphql googleLogin mutation API failed with codes=FORBIDDEN, probably this user is deactivated',
},
[ERROR_CODE.ER0013]: {
[ER0013]: {
external: LOGIN_ERROR_MSG,
internal: 'Unknown error during Graphql googleLogin mutation',
},
[ERROR_CODE.ER0014]: {
[ER0014]: {
external: LOGIN_ERROR_MSG,
internal:
'Graphql facebookLogin mutation API failed with codes=UNAUTHENTICATED',
},
[ERROR_CODE.ER0015]: {
[ER0015]: {
external: LOGIN_ERROR_MSG,
internal:
'Graphql facebookLogin mutation API failed with codes=FORBIDDEN, probably this user is deactivated',
},
[ERROR_CODE.ER0016]: {
[ER0016]: {
external: LOGIN_ERROR_MSG,
internal: 'Unknown error during Graphql facebookLogin mutation',
},
[ERROR_CODE.ER0018]: {
[ER0018]: {
internal: 'Unexpected error during submitting form data',
},
[ERROR_CODE.ER0019]: {
[ER0019]: {
internal: 'Not submittable',
},
[ERROR_CODE.ER0020]: {
[ER0020]: {
internal: 'Submit work experience failed',
},
[ERROR_CODE.ER0021]: {
[ER0021]: {
internal: 'Warning without validation failure',
},
};

0 comments on commit d614937

Please sign in to comment.