-
Notifications
You must be signed in to change notification settings - Fork 40
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 #177 from openkfw/131-error-handling
Describe approach to error handling and fix NotAuthorized HTTP status code
- Loading branch information
Showing
39 changed files
with
324 additions
and
132 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 |
---|---|---|
@@ -1,23 +1,35 @@ | ||
import { isArray } from "util"; | ||
|
||
import Intent from "../../../authz/intents"; | ||
import { Ctx } from "../../../lib/ctx"; | ||
import { BusinessEvent } from "../business_event"; | ||
|
||
export class NotAuthorized extends Error { | ||
private readonly target?: object; | ||
|
||
constructor( | ||
private readonly ctx: Ctx, | ||
private readonly userId: string, | ||
private readonly businessEvent?: BusinessEvent, | ||
private readonly intent?: Intent, | ||
private readonly intent: Intent | Intent[], | ||
target?: object, | ||
) { | ||
super( | ||
`User ${userId} is not authorized${ | ||
businessEvent ? ` to apply ${businessEvent.type}` : ` to invoke ${intent}` | ||
}.`, | ||
`user ${userId} is not authorized for ${ | ||
isArray(intent) ? `any of ${intent.join(", ")}` : intent | ||
}`, | ||
); | ||
|
||
// This allows us to identify this error in a chain of errors later on: | ||
this.name = "NotAuthorized"; | ||
|
||
// Maintains proper stack trace for where our error was thrown (only available on V8): | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, NotAuthorized); | ||
} | ||
|
||
// Removing trace events as they're not needed and spam the log output when printed: | ||
if (target !== undefined && (target as any).log !== undefined) { | ||
delete (target as any).log; | ||
} | ||
this.target = target; | ||
} | ||
} |
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
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
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
Oops, something went wrong.