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

錯誤處理 #17

Open
outshaker opened this issue Sep 30, 2021 · 0 comments
Open

錯誤處理 #17

outshaker opened this issue Sep 30, 2021 · 0 comments

Comments

@outshaker
Copy link
Owner

https://github.com/outshaker/foodmap-backend/blob/dev/express/errorMessage.js
目前還不確定前端頁面那邊怎麼處理後端 API 回傳的錯誤訊息

模擬情境:

前端因為未登入而得到一個錯誤

{
    ok: 0,
    message: 'Please login to continue.',
}

後續處理:

  • 直接顯示在頁面上 => 內容為英文,對使用者可能不太友善。如果要轉換成使用者可以看的訊息,勢必又需要另外一張轉換表,那可能一開始直接存成中文會更好一些。

  • 判斷錯誤種類 => 到時候大概會這樣判斷 if (result.message === errorMessage.needLogin.message) 假如說 message 沒有要顯示給使用者看的話,那或許可以再簡化一些,然後再比對就好。

簡化版:

const errorMessage = {
  userNotFound: 'invalid username or password.',
  userIdNotFound: 'invalid userId.',
  needLogin: 'Please login to continue.',
  unauthorized:'unauthorized.',
  fetchFail: 'Fail to fetch.',
  missingParameter: 'Please input query parameter.',
  general: 'please try again.',
  usernameError: 'username format incorrect.',
  passwordNotSame: 'please check your password is correct and submit again.',
  passwordError: 'password format incorrect.',
  duplicateUsernameOrEmail: 'duplicate username or email.',
  noParameter: 'Please input query parameter.'
}

if (result.message === errorMessage.needLogin) 只要前後端共用同一份 errorMessage 就好,其實還蠻方便的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant