CenterForDigitalHumanities / CenterForDigitalHumanities/rerum_server_nodejs

Standardize route error handling to use createExpressError

Aperta
#261 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
enhancement
Lingua principale
JavaScript
Stelle
3
Fork
6
Merge medio
2g 47m
PR unite (30g)
4

Descrizione

## Description

The POST handlers in the route files use an unconventional pattern of passing the `res` object directly to `next()` as the error argument:

```javascript
res.statusMessage = 'Improper request method for updating, please use PATCH to add new keys to this object.'
res.status(405)
next(res)
```

This works because `rest.messenger` reads `err.statusMessage` and `err.statusCode` from whatever object is passed, and the `res` object happens to have those properties. However, it creates a confusing situation where `err === res` inside the error handler.

The rest of the codebase uses `next(utils.createExpressError({...}))` for error handling (see `rest.js`), which is the standard Express convention.

Standardize these routes to use the same `createExpressError` pattern as the rest of the codebase:

```javascript
if (!rest.checkPatchOverrideSupport(req, res)) {
return next(utils.createExpressError({
statusCode: 405,
statusMessage: 'Improper request method for updating, please use PATCH to add new keys to this object.'
}))
}
controller.patchSet(req, res, next)
```

## Context

Found during static review of #206. Out of scope for that PR since it's a pre-existing pattern.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia da rest.js per esaminare il pattern esistente di utils.createExpressError, quindi cerca nei file delle route gli handler POST che assegnano statusMessage o statusCode a res prima di chiamare next(res). Sostituisci ogni percorso di errore corrispondente con il pattern standard dell’oggetto errore e conferma che tutti questi handler ora passino gli errori creati a next().

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript
Ambito
backend
Tipo di issue
Refactoring
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
68/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.