Skip to content

Commit

Permalink
Add GoneError, a bit of docs for the errors
Browse files Browse the repository at this point in the history
  • Loading branch information
floscher committed May 16, 2023
1 parent bb97435 commit 0f6e5aa
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 4 deletions.
3 changes: 3 additions & 0 deletions server/src/errors/BadRequestError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { BaseError } from "./BaseError.js";
import { HttpStatusCode } from "./HttpStatusCode.js";

/**
* HTTP error [400: Bad request](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400)
*/
export class BadRequestError extends BaseError {
constructor(description = "Bad request") {
super(HttpStatusCode.BAD_REQUEST, description, true);
Expand Down
7 changes: 7 additions & 0 deletions server/src/errors/ForbiddenError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { BaseError } from "./BaseError.js";
import { HttpStatusCode } from "./HttpStatusCode.js";

/**
* HTTP error [403: Forbidden](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403).
*
* Use this for when a user lacks permissions to do something.
* If there are no (or incomplete) credentials, so the logged-in user can't even
* be determined, then use {@link UnauthorizedError} instead.
*/
export class ForbiddenError extends BaseError {
constructor(description = "Forbidden") {
super(HttpStatusCode.FORBIDDEN, description, true);
Expand Down
11 changes: 11 additions & 0 deletions server/src/errors/GoneError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BaseError } from "./BaseError.js";
import { HttpStatusCode } from "./HttpStatusCode.js";

/**
* HTTP error [410: Gone](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/410)
*/
export class GoneError extends BaseError {
constructor(description = "Gone") {
super(HttpStatusCode.GONE, description, true);
}
}
4 changes: 4 additions & 0 deletions server/src/errors/HttpStatusCode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/**
* [HTTP status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
*/
export enum HttpStatusCode {
OK = 200,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
FORBIDDEN = 403,
NOT_FOUND = 404,
GONE = 410,
INTERNAL_SERVER = 500,
}
3 changes: 3 additions & 0 deletions server/src/errors/InternalServerError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { BaseError } from "./BaseError.js";
import { HttpStatusCode } from "./HttpStatusCode.js";

/**
* HTTP error [500: Internal Server Error](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500)
*/
export class InternalServerError extends BaseError {
constructor(isOperational = true, description = "Internal server error") {
super(HttpStatusCode.INTERNAL_SERVER, description, isOperational);
Expand Down
3 changes: 3 additions & 0 deletions server/src/errors/NotFoundError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { BaseError } from "./BaseError.js";
import { HttpStatusCode } from "./HttpStatusCode.js";

/**
* HTTP error [404: Not Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404)
*/
export class NotFoundError extends BaseError {
constructor(description = "Not found") {
super(HttpStatusCode.NOT_FOUND, description, true);
Expand Down
6 changes: 6 additions & 0 deletions server/src/errors/UnauthorizedError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { BaseError } from "./BaseError.js";
import { HttpStatusCode } from "./HttpStatusCode.js";

/**
* HTTP error [401: Unauthorized](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401).
*
* Use this for when no (or incomplete) credentials are provided.
* If there are credentials, but not enough permissions, use {@link ForbiddenError} instead.
*/
export class UnauthorizedError extends BaseError {
constructor(description = "Unauthorized") {
super(HttpStatusCode.UNAUTHORIZED, description, true);
Expand Down
8 changes: 4 additions & 4 deletions server/src/routes/posts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DraftResponseDto, EditPostRequestDto, NewPostRequestDto, permissionsForUser, PostRequestDto } from "@fumix/fu-blog-common";
import { GoneError } from "../errors/GoneError.js";
import express, { NextFunction, Request, Response, Router } from "express";
import { In } from "typeorm";
import { AppDataSource } from "../data-source.js";
Expand Down Expand Up @@ -90,8 +91,7 @@ router.get("/:id(\\d+$)", async (req: Request, res: Response, next) => {
})
.then((result) => {
if (result === null) {
res.status(410).json({ data: null });
// throw new NotFoundError("No post found with id " + req.params.id);
next(new GoneError("No post found with id " + req.params.id));
} else {
res.status(200).json({ data: result });
}
Expand Down Expand Up @@ -278,7 +278,7 @@ router.post("/:id(\\d+$)", authMiddleware, multipleFilesUpload, async (req: Requ
.then((updateResult) => {
// TODO: Optimize, so unchanged attachments are not deleted and re-added
manager.getRepository(AttachmentEntity).delete({ post: { id: post.id } });
//manager.getRepository(AttachmentEntity).insert(extractUploadFiles(req).map((it) => convertAttachment(post, it)));
// manager.getRepository(AttachmentEntity).insert(extractUploadFiles(req).map((it) => convertAttachment(post, it)));
// tagsToUseInPost.forEach((tag) => {
// manager.getRepository(PostEntity).createQueryBuilder().relation(PostEntity, "tags").add(tag);
// });
Expand Down Expand Up @@ -322,7 +322,7 @@ router.get("/:id(\\d+)/og-image", async (req: Request, res: Response, next) => {
AppDataSource.getRepository(PostEntity)
.findOne({ where: { id: +req.params.id } })
.then((post) => {
if (post) {
if (post && post.id) {
res.status(200).write(generateShareImage(post.title, post.createdAt));
res.end();
} else {
Expand Down

0 comments on commit 0f6e5aa

Please sign in to comment.