Added better http error code in rest api

This commit is contained in:
didier amyot committed 2023-06-13 11:51:42 -04:00
1 parent 95e58a67df
commit b70628264e
3 files changed
+366 -6

No files matched your search

+5
View File
@@ -1,3 +1,5 @@
// deno std
export { Buffer, StringReader } from 'https://deno.land/std@0.191.0/io/mod.ts';
export {
iterateReader,
@@ -7,4 +9,7 @@ export {
readerFromStreamReader,
TextLineStream,
} from 'https://deno.land/std@0.191.0/streams/mod.ts';
export {Status} from "https://deno.land/std@0.191.0/http/http_status.ts";
// oak
export { Application, Context, Router } from 'https://deno.land/x/oak/mod.ts';
+18 -6
View File
@@ -1,9 +1,7 @@
import { Context, Router, TextLineStream, readableStreamFromReader } from './deps.ts';
import { Context, Router, Status} from './deps.ts';
import { FileMetaData, FileService } from './services/mod.ts';
import { readerFromStreamReader } from './deps.ts';
import { asyncIterableToArray } from './utils.ts';
import { Status } from "../vendor/deno.land/std@0.178.0/http/http_status.ts";
import { WaveFrontServiceImpl } from "./services/wavefrontService.ts";
import { Vector3D, WaveFrontService } from './services/interfaces.ts';
type Handler = (ctx: Context) => void;
@@ -41,6 +39,12 @@ function createFileHandlerFactory(fileService: FileService): Handler {
const path = untypedCtx.request.url.searchParams.get('path');
if(!path){
ctx.response.status = Status.BadRequest;
ctx.response.body = `Requires a "path" url parameter.`
return;
}
if(!ctx.request.hasBody){
ctx.response.status = Status.BadRequest;
ctx.response.body = `Requires a body to the POST request (the file basically).`
return;
}
const reqBody = await ctx.request.body({ type: 'stream' }).value;
@@ -57,11 +61,14 @@ function renameFileHandlerFactory(fileService: FileService): Handler {
const reqBody = await ctx.request.body().value;
const newPath = reqBody.path;
if(!reqBody.path){
// nothing to do.
ctx.response.status = Status.OK;
return;
}
const ret = await fileService.renameById(fileId, newPath);
if(ret === null){
ctx.response.status = Status.NotFound;
ctx.response.body = `No File with id "${fileId}".`
return
}
@@ -77,6 +84,7 @@ function getFileHandlerFactory(fileService: FileService): Handler {
const fm = await fileService.getMetaDataByID(fileId);
if (fm == null) {
ctx.response.status = 404;
ctx.response.body = `No File with id "${fileId}".`
return;
}
ctx.response.status = 200;
@@ -91,6 +99,7 @@ function getDownloadFileHandlerFactory(fileService: FileService): Handler {
const reader = await fileService.getContentById(fileId);
if (reader == null) {
ctx.response.status = 404;
ctx.response.body = `No File with id "${fileId}".`
return;
}
ctx.response.status = 200;
@@ -120,7 +129,8 @@ function getDownloadTransformedFileHandlerFactory(fileService: FileService, wave
const fileId = untypedCtx.params.fileId;
const reader = await fileService.getContentById(fileId);
if (reader == null) {
ctx.response.status = 404;
ctx.response.status = Status.NotFound;
ctx.response.body = `No File with id "${fileId}".`
return;
}
@@ -128,6 +138,7 @@ function getDownloadTransformedFileHandlerFactory(fileService: FileService, wave
const translate = getTranslateVector(untypedCtx.request.url.searchParams);
if(scale === null || translate === null){
ctx.response.status = Status.BadRequest;
ctx.response.body = `Bad scale of translate parameters.`
return;
}
ctx.response.status = 200;
@@ -138,10 +149,11 @@ function getDownloadTransformedFileHandlerFactory(fileService: FileService, wave
function deleteFileHandlerFactory(fileService: FileService): Handler {
return async (ctx: Context) => {
const untypedCtx: any = ctx;
const path = untypedCtx.params.fileId;
const isDeleted = await fileService.deleteById(path);
const fileId = untypedCtx.params.fileId;
const isDeleted = await fileService.deleteById(fileId);
if (!isDeleted) {
ctx.response.status = 404;
ctx.response.body = `No File with id "${fileId}".`
return;
}
ctx.response.status = 200;