-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from sima-land/38-refactor-for-redirects
Шаг 77 #38 Рефакторинг для редиректов
- Loading branch information
Showing
5 changed files
with
64 additions
and
56 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
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,38 @@ | ||
import { ResponseError } from '../../../http'; | ||
import { LogLevel } from '../../../log'; | ||
|
||
/** | ||
* Функция форматирования ошибки обработчика запроса. | ||
* @param error Ошибка. | ||
* @return Формат ответа и лога ошибки. | ||
*/ | ||
export function formatHandlerError(error: unknown) { | ||
let logLevel: LogLevel | null = 'error'; | ||
let message: string; | ||
let statusCode = 500; // по умолчанию, если на этапе подготовки страницы что-то не так, отдаем 500 | ||
let redirectLocation: string | null = null; | ||
|
||
if (error instanceof Error) { | ||
message = error.message; | ||
|
||
if (error instanceof ResponseError) { | ||
statusCode = error.statusCode; | ||
redirectLocation = error.redirectLocation; | ||
logLevel = error.logLevel; | ||
} | ||
} else { | ||
message = String(error); | ||
} | ||
|
||
return { | ||
response: { | ||
status: statusCode, | ||
body: message, | ||
redirectLocation, | ||
}, | ||
log: { | ||
level: logLevel, | ||
message, | ||
}, | ||
}; | ||
} |
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