1.0.0 • Published 1 year ago
@decorators-ts/exceptions-logs v1.0.0
@DECORATORS-TS/EXCEPTION-LOGS
version: 0.0.6
:bookmark_tabs: About the Project
Based on design patterns decorators, this functionality will allow you to implement an exception log decorator that brings you custom information...
:pushpin: How to install and run the project
Install
yarn add -D @decorators-ts/exceptions-logs
or
npm install -D @decorators-ts/exceptions-logs
:question: How does this help me?
As seen in the images above, the decorator implements custom logs, thus, sending you the controller execution time and also the error reason, if it happens
How to use?
:globe_with_meridians: Exception Decorators
import { Execpetion } from '@decorators-ts/exceptions-logs'
on the top line of your class function, invoke the method
@Execpetion({
methodName: 'DevelopmentController',
})
parameters
- methodName: (String) - Name of the class or function to be executed
- execeptionError: (Error | any) - By default it will return a new Error('Internal Server Error'), but if you want to customize the return in case of an error, make the notation in this parameter
return
- the event tasks will be displayed in your console, thus delivering customized information in case of error or success
EXAMPLE
import { Execpetion } from '@decorators-ts/exceptions-logs'
interface IDevelpmentController {
running: (into: string) => Promise<string>
}
class DevelopmentController implements IDevelpmentController {
@Execpetion({
methodName: 'DevelopmentController',
})
public async running (arg: string): Promise<string> {
return arg
}
}
my console in execution success
[INFO - LOGGER ID: kdhp155] EXECUTION RUNNING - 13:55:23 - 23/10/2022 13:55:23 | - [RUNNING] DevelopmentController
[INFO - LOGGER ID: kdhp155] EXECUTION SUCCESS - 13:55:23 - 23/10/2022 13:55:23 | - [FINALLY] DevelopmentController - [TASK EVENT: 0.005 ms]
my console in execution fails
[INFO - LOGGER ID: 91by155] EXECUTION RUNNING - 13:55:28 - 23/10/2022 13:55:28 | - [RUNNING] DevelopmentController
[INFO - LOGGER ID: 91by155] EXECUTION FAILED - 13:55:28 - 23/10/2022 13:55:28 | - [FINALLY] DevelopmentController - [TASK EVENT LOGGER ERROR: args is not defined]
[INFO - LOGGER ID: 91by155] EXECUTION FAILED - 13:55:28 - 23/10/2022 13:55:28 | - [FINALLY] DevelopmentController - [TASK EVENT DEBUG ERROR : ['YOUT PATH OR FILE ERROR']]
:globe_with_meridians: Parameters Required Decorators
import { ValidateRequired, Required } from '@decorators-ts/exceptions-logs'
parameters
- this new update the field validation resources are more advanced!!
- indicate the required input parameters in an array
- identify as @Required in the parameter receiving field
return
- in eventual confirmation of mandatory parameter, it will generate an error
EXAMPLES
@ValidateRequired([
{
name: 'name',
type: 'string',
required: true,
maxLength: 11
}
])
@ValidateRequired([
{
isArray: true,
typeOfArray: 'object',
nameOfArray: 'users',
fieldsArray: [{ name: ['string', true], age: ['number', false] }]
}
])
- Use different validators, which even validate objects inside arrays
@ValidateRequired([
{
isArray: true,
typeOfArray: 'object',
nameOfArray: 'users',
fieldsArray: [{ name: ['string', true], age: ['number', false] }]
}
{
name: 'name',
type: 'string',
required: true,
maxLength: 11
}
])
public async running (@Required args: IArgs): Promise<void> {}
interface IMyPropsParameters {
users?: any
id: number
age: number
}
class parameters {
@ValidateRequired([
{
isArray: true,
typeOfArray: 'object',
nameOfArray: 'users',
fieldsArray: [{ name: ['string', true], age: ['number', false] }]
},
{
name: 'id',
type: 'number',
required: true
},
{
name: 'age',
type: 'number',
required: true
}
])
async paramters (@Required props: IMyPropsParameters) {
}
}
class service {
async validated () {
try {
await new parameters().paramters({
users: [
{ name: 'Christian' }
],
id: 4569,
age: 4
})
} catch (e) {
console.error(e)
}
}
}
new service().validated()
.then()
.catch()