2016-10-06 9 views
1

nodeJSで非同期を使用しています。最終コールバックでエラーを処理して、それを角度コントローラに戻そうとしています。nodeJsのクライアント側にエラーを送信中にエラーが発生しました

function (err, data) { 
     if (err) { 
      console.log(err); 
      res.status(500).send({ err : err}); 
     } 
     else { 
      res.json({data: data}); 
     } 
    }); 

これでコンソールのエラーが発生しました。

[Error: Username is already in use] 

角度コントローラでこの特定のエラーが発生することはありません。

res.status(500).send({ err : err[0]}); 
     res.status(500).send({ err : err.Error}); 

これは私のフロントエンドで得られるものです。

Object {data: Object, status: 500, config: Object, statusText: "Internal Server Error"} 
config 
: 
Object 
data 
: 
Object 
err 
: 
Object 
__proto__ 
: 
Object 
__proto__ 
: 
Object 
headers 
: 
(d) 
status 
: 
500 
statusText 
: 
"Internal Server Error" 

どのように私のフロントエンドにそのユーザー名を使用エラーにすることができますか?

+0

何dあなたの機能は何ですか? – abdulbarik

+0

res.status(500).send({err:err});ここで、errが[Error:Username is already in use]のエラーを送信しようとしていますが、フロントエンドには行かない。 –

+0

エラー500が表示されない状態を返さない理由は、データエラーではないサーバーエラーのためです。 – madalinivascu

答えて

1

500エラーは通常、サーバーエラーのために予約されていますが、説明したようなシナリオではありません。サーバーエラーは、サーバーで処理し、フロントエンドに優雅に提示する必要があります。クライアントのエラーは400秒以内でなければなりません。注

409 Conflict The request could not be completed due to a conflict with the current state of the resource. This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body SHOULD include enough information for the user to recognize the source of the conflict. Ideally, the response entity would include enough information for the user or user agent to fix the problem; however, that might not be possible and is not required.

Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't complete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type.

:多くのためのHTTP Status codes

res.status(409).json({error: "Username already exists"); 

ルック:あなたは409400をしようとしないのはなぜまた、良いプラクティスとして、必ずあなたを返す作りますres.の機能は、制御の予測可能なフローのために次のようになります。

return res.status(409).json({error: "Username already exists"); 
+0

おかげでとにかく –

+0

@sacDahal純粋にRESTfulな観点から、私はあなたが '409'または' 400'と一緒に行かなければならないと思います。それはニックピッキングだけど、RESTは素晴らしいです:)私も同様に答えを更新しました – nikjohn

関連する問題