Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
¿Cuál es una forma adecuada de manejar los errores de Node js a Angular 2?

Tengo una tarea sencilla. Digamos que ese usuario puede agregar una noticia en un formulario que tiene dos campos, título y contenido. El campo de título debe ser llenado. Puedo verificarlo en el lado del cliente pero también en el lado del servidor, por ejemplo:

noticias.js

 var _this = module.exports = { add: (news) => { return new Promise((resolve, reject) => { if(!news.title) reject('Title must be filled!') //rest of code }) } }

este error se puede detectar en la aplicación principal.js

 app.post('/add_news', (req, resp) => { var news = req.body.data; _news.add(news).then(result => { //send success }).catch(err => { //send error }) })

y quiero atraparlo en el lado del cliente:

 this.newsService.add(news).subscribe( result => { //here ok }, error => { //here errors })

¿Cómo hacerlo? ¿Qué debo enviar del lado del servidor al cliente? resp.status no funciona como quiero. ¿Es tal vez una mejor solución?

Saludos

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

En general, su API debería devolver algún código de error (número o cadena, no importa), y su frontend angular debería manejar ese código de error.

noticias.js

 if (!news.title) { reject({statusCode: 404, errorCode: 'MISSING_TITLE'}); }

aplicación.js

 _news.add(news).then(result => { //send success }).catch(err => { // process only errors with `statusCode` and `errorCode` if (err.statusCode && err.errorCode) { resp.status(err.statusCode); resp.json(err.errorCode); } else { // otherwise return generic error resp.status(500); resp.json('API_ERROR'); } })

Luego en el lado del cliente:

 this.newsService.add(news).subscribe( result => { //here ok }, error => { if (error === 'MISSING_TITLE') { alert('Title field is required!'); // or modify the $scope to show some error message in your template } else { alert('An error occurred'); // fallback to general error message when some other message comes } })
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!