-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import express, { NextFunction } from 'express' | ||
export function sendErrorResponse(response: express.Response, statusCode: number, message: string | object, error?: Error) { | ||
console.log(`sendErrorResponse: ${message}`) | ||
if (error) { | ||
console.log(JSON.stringify(error)) | ||
} | ||
if (response.headersSent) { | ||
console.log(`sendErrorResponse headers already sent`) | ||
return | ||
} | ||
if (response.headersSent) { | ||
return | ||
} | ||
response.statusCode = statusCode | ||
if (typeof message === 'string' && !message.startsWith('{')) { | ||
message = { error: message } | ||
} | ||
if (typeof message === 'string' && message.startsWith('{')) { | ||
return response.status(statusCode).end(message) | ||
} | ||
return response.status(statusCode).json(message) | ||
} | ||
|
||
export const jsonErrorHandler = (err: any, req: express.Request, res: express.Response, next: NextFunction) => { | ||
const statusCode: number = 'statusCode' in err ? err.statusCode : 500 | ||
const errorMsg = typeof err === 'string' ? err : err.message | ||
if (res.headersSent) { | ||
console.log('Headers already sent, when calling error handler. Will defer to next error handler') | ||
console.log(`Error was: ${JSON.stringify(err)}`) | ||
return next(err) | ||
} | ||
return sendErrorResponse(res, statusCode, errorMsg, typeof err !== 'string' ? err : undefined) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,6 @@ | ||
import express, { NextFunction } from 'express' | ||
import process from 'process' | ||
|
||
export function env(key?: string, prefix?: string): string | undefined { | ||
if (!key) { | ||
return | ||
} | ||
return process.env[`${prefix ? prefix.trim() : ''}${key}`] | ||
} | ||
|
||
export function sendErrorResponse(response: express.Response, statusCode: number, message: string | object, error?: Error) { | ||
console.log(message) | ||
if (error) { | ||
console.log(error) | ||
} | ||
response.statusCode = statusCode | ||
if (typeof message === 'string' && !message.startsWith('{')) { | ||
message = { error: message } | ||
} | ||
if (typeof message === 'string' && message.startsWith('{')) { | ||
return response.status(statusCode).end(message) | ||
} | ||
return response.status(statusCode).json(message) | ||
} | ||
|
||
export const jsonErrorHandler = (err: any, req: express.Request, res: express.Response, next: NextFunction) => { | ||
if (res.headersSent) { | ||
return next(err) | ||
} | ||
return sendErrorResponse(res, 500, err.message, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
export * from './entra-id-auth' | ||
export * from './static-bearer-auth' | ||
export * from './auth-utils' | ||
export * from './builders' | ||
export * from './express-builders' | ||
export * from './types' | ||
export { sendErrorResponse, jsonErrorHandler, env } from './functions' | ||
export { sendErrorResponse, jsonErrorHandler } from './express-utils' | ||
export * from './functions' |