Skip to content

Commit

Permalink
Merge pull request #1 from halvardssm/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
halvardssm authored Jul 12, 2020
2 parents 8dac7e4 + d04f975 commit 0199f8f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 31 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: CI

env:
DENO_VERSION: 1.1.3

on:
push:
branches:
Expand All @@ -21,8 +24,8 @@ jobs:
- name: Install deno
uses: denolib/setup-deno@master
with:
deno-version: 1.0.2
deno-version: 1.1.3
- name: Check formatting
run: deno fmt --check
- name: Run tests
run: deno test --allow-read --allow-write
run: make test
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test:
deno test --allow-read --allow-write
fmt:
deno fmt src *.ts
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Oak Middleware Error & Logging

![CI](https://github.com/halvardssm/oak-middleware-error-logger/workflows/CI/badge.svg)
[![(Deno)](https://img.shields.io/badge/deno-1.0.2-green.svg)](https://deno.land)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/raw.githubusercontent.com/halvardssm/oak-middleware-error-logger/master/mod.ts)
[![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/halvardssm/oak-middleware-error-logger/ci/master?style=flat-square&logo=github)](https://github.com/halvardssm/oak-middleware-error-logger/actions?query=branch%3Amaster+workflow%3ACI)
[![(Deno)](https://img.shields.io/badge/deno-v1.1.3-green.svg?style=flat-square&logo=deno)](https://deno.land)
[![deno doc](https://img.shields.io/badge/deno-doc-blue.svg?style=flat-square&logo=deno)](https://doc.deno.land/https/raw.githubusercontent.com/halvardssm/oak-middleware-logger/master/mod.ts)

Oak middleware for error handling and logging

Expand All @@ -17,11 +17,18 @@ Oak middleware for error handling and logging

const app = new Application();

app.use(errorHandlerMiddleware<Middleware>({
fallback: (err, ctx) => {
throw new Error(err as string);
},
}));
app.use(
errorHandlerMiddleware<Middleware>({
fallback: async (err, ctx) => {
await logger({ logInfo: err.toString() })
if (isHttpError(err)) {
ctx.throw(err.status, err.message)
} else {
ctx.throw(ErrorStatus.InternalServerError, "Server error")
}
},
})
);

await app.listen(appOptions);
```
Expand Down
8 changes: 4 additions & 4 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export {
errorCallback,
ErrorCallback,
errorHandlerMiddleware,
errorHandlerMiddlewareOptions,
ErrorHandlerMiddlewareOptions,
} from "./src/errorMiddleware.ts";
export {
logger,
loggerOptions,
loggerReturnT,
LoggerOptions,
LoggerReturnT,
loggerSync,
} from "./src/logger.ts";
12 changes: 6 additions & 6 deletions src/errorMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Context, HttpError, Middleware, RouterMiddleware } from "../deps.ts";

export type errorCallback = (
export type ErrorCallback = (
err: HttpError | string | number,
ctx: Context,
) => void;
) => Promise<void>;

export type errorHandlerMiddlewareOptions = {
fallback: errorCallback; // Fallback function for handling all errors which are not handled by the cases
cases?: Record<number | string, errorCallback>; // Record of all cases with a callback to handle errors
export type ErrorHandlerMiddlewareOptions = {
fallback: ErrorCallback; // Fallback function for handling all errors which are not handled by the cases
cases?: Record<number | string, ErrorCallback>; // Record of all cases with a callback to handle errors
};

export const errorHandlerMiddleware = <
T extends RouterMiddleware | Middleware = Middleware,
>(options: errorHandlerMiddlewareOptions): T => {
>(options: ErrorHandlerMiddlewareOptions): T => {
const { cases, fallback } = options;

const core: Middleware = async (ctx, next) => {
Expand Down
22 changes: 11 additions & 11 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname } from "../deps.ts";

export type loggerOptions = {
export type LoggerOptions = {
logInfo: string | object; // Log text or object
fileName?: string; // Custom filename, will default to `.log/mm-yyyy.txt` e.g `.log/04-2020.txt`
prepend?: string; // Custom prepend for log line, will default to iso date e.g. `2020-05-25T17:38:42.483Z: `
Expand All @@ -9,21 +9,21 @@ export type loggerOptions = {
customFormatting?: (log: string) => string; // Add custom formatting to logInfo
};

export type mkdirArgsT = [
export type MkdirArgsT = [
string,
Deno.MkdirOptions | undefined,
];
export type writeFileArgsT = [
export type WriteFileArgsT = [
string,
Uint8Array,
Deno.WriteFileOptions | undefined,
];
export type loggerReturnT = [
mkdirArgsT,
writeFileArgsT,
export type LoggerReturnT = [
MkdirArgsT,
WriteFileArgsT,
];

const loggerCore = (options: loggerOptions) => {
const loggerCore = (options: LoggerOptions) => {
const date = new Date();
const encoder = new TextEncoder();
const logData = typeof options.logInfo === "string"
Expand All @@ -46,15 +46,15 @@ const loggerCore = (options: loggerOptions) => {
return [[dirname(name), mkdirOptions], [name, data, writeFileOptions]];
};

export const logger = async (options: loggerOptions): Promise<void> => {
const args = loggerCore(options) as loggerReturnT;
export const logger = async (options: LoggerOptions): Promise<void> => {
const args = loggerCore(options) as LoggerReturnT;

await Deno.mkdir(...args[0]);
await Deno.writeFile(...args[1]);
};

export const loggerSync = (options: loggerOptions): void => {
const args = loggerCore(options) as loggerReturnT;
export const loggerSync = (options: LoggerOptions): void => {
const args = loggerCore(options) as LoggerReturnT;

Deno.mkdirSync(...args[0]);
Deno.writeFileSync(...args[1]);
Expand Down

0 comments on commit 0199f8f

Please sign in to comment.