Ver a proveniência

Add error handling

Check if service can initialize data source before making requests.
master
TheoryOfNekomata há 8 meses
ascendente
cometimento
284a5cd3bf
1 ficheiros alterados com 35 adições e 22 eliminações
  1. +35
    -22
      src/handlers.ts

+ 35
- 22
src/handlers.ts Ver ficheiro

@@ -138,15 +138,20 @@ export const handleGetCollection: Middleware = ({
};
}

await theResource.dataSource.initialize();
const resData = await theResource.dataSource.getMultiple(); // TODO paginated responses per resource
const theFormatted = theSerializerPair.serialize(resData);
try {
await theResource.dataSource.initialize();
const resData = await theResource.dataSource.getMultiple(); // TODO paginated responses per resource
const theFormatted = theSerializerPair.serialize(resData);

res.writeHead(constants.HTTP_STATUS_OK, {
'Content-Type': theMediaType,
'X-Resource-Total-Item-Count': resData.length
});
res.end(theFormatted);
res.writeHead(constants.HTTP_STATUS_OK, {
'Content-Type': theMediaType,
'X-Resource-Total-Item-Count': resData.length
});
res.end(theFormatted);
} catch {
res.statusCode = constants.HTTP_STATUS_INTERNAL_SERVER_ERROR;
res.end();
}

return {
handled: true
@@ -201,23 +206,31 @@ export const handleGetItem: Middleware = ({
};
}

await theResource.dataSource.initialize();
const singleResDatum = await theResource.dataSource.getSingle(mainResourceId);
if (singleResDatum) {
const theFormatted = theSerializerPair.serialize(singleResDatum);
res.writeHead(constants.HTTP_STATUS_OK, { 'Content-Type': theMediaType });
res.end(theFormatted);
try {
await theResource.dataSource.initialize();
const singleResDatum = await theResource.dataSource.getSingle(mainResourceId);
if (singleResDatum) {
const theFormatted = theSerializerPair.serialize(singleResDatum);
res.writeHead(constants.HTTP_STATUS_OK, {'Content-Type': theMediaType});
res.end(theFormatted);
return {
handled: true
};
}

res.statusCode = constants.HTTP_STATUS_NOT_FOUND;
res.statusMessage = `${theResource.itemName} Not Found`;
res.end();
return {
handled: true
};
} catch {
res.statusCode = constants.HTTP_STATUS_INTERNAL_SERVER_ERROR;
res.end();
return {
handled: true
};
}

res.statusCode = constants.HTTP_STATUS_NOT_FOUND;
res.statusMessage = `${theResource.itemName} Not Found`;
res.end();
return {
handled: true
};
};


@@ -271,8 +284,8 @@ export const handleDeleteItem: Middleware = ({
}
}

await theResource.dataSource.initialize();
try {
await theResource.dataSource.initialize();
const response = await theResource.dataSource.delete(mainResourceId);
if (typeof response !== 'undefined' && !response && theResource.throws404OnDeletingNotFound) {
res.statusCode = constants.HTTP_STATUS_NOT_FOUND;


Carregando…
Cancelar
Guardar