Skip to content

Commit

Permalink
N21-1631 change ctl error handling (#4947)
Browse files Browse the repository at this point in the history
* add new ContextExternalToolNameAlreadyExistsLoggableException
* adjust ToolParameterDuplicateLoggableException
* adjust ToolParameterRequiredLoggableException
* adjust ToolParameterTypeMismatchLoggableException
* adjust ToolParameterUnknownLoggableException
* adjust ToolParameterValueMissingLoggableException
* adjust ToolParameterValueRegexLoggableException
  • Loading branch information
MBergCap authored Apr 24, 2024
1 parent 291bd62 commit 8647f31
Show file tree
Hide file tree
Showing 34 changed files with 302 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ContextExternalToolNameAlreadyExistsLoggableException } from './context-external-tool-name-already-exists.loggable-exception';

describe(ContextExternalToolNameAlreadyExistsLoggableException.name, () => {
describe('getLogMessage', () => {
const setup = () => {
const exception: ContextExternalToolNameAlreadyExistsLoggableException =
new ContextExternalToolNameAlreadyExistsLoggableException('toolid', 'toolname');

return {
exception,
};
};

it('should return log message', () => {
const { exception } = setup();

const result = exception.getLogMessage();

expect(result).toEqual({
type: 'CONTEXT_EXTERNAL_TOOL_NAME_ALREADY_EXISTS',
message:
'A tool with the same name is already assigned to this course. Tool names must be unique within a course.',
stack: exception.stack,
data: {
toolId: 'toolid',
toolName: 'toolname',
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { BusinessError } from '@shared/common';
import { ErrorLogMessage, Loggable, LogMessage, ValidationErrorLogMessage } from '@src/core/logger';
import { HttpStatus } from '@nestjs/common';
import { EntityId } from '@shared/domain/types';

export class ContextExternalToolNameAlreadyExistsLoggableException extends BusinessError implements Loggable {
constructor(private readonly toolId: EntityId | undefined, private readonly toolName: string | undefined) {
super(
{
type: 'CONTEXT_EXTERNAL_TOOL_NAME_ALREADY_EXISTS',
title: 'Toolname already exists',
defaultMessage:
'A tool with the same name is already assigned to this course. Tool names must be unique within a course.',
},
HttpStatus.BAD_REQUEST,
{
toolId,
toolName,
}
);
}

getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage {
return {
type: this.type,
message: this.message,
stack: this.stack,
data: {
toolId: this.toolId,
toolName: this.toolName,
},
};
}
}
1 change: 1 addition & 0 deletions apps/server/src/modules/tool/common/domain/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { ToolParameterUnknownLoggableException } from './tool-parameter-unknown.
export { ToolParameterValueRegexLoggableException } from './tool-parameter-value-regex.loggable-exception';
export { ToolParameterTypeMismatchLoggableException } from './tool-parameter-type-mismatch.loggable-exception';
export { ToolParameterValueMissingLoggableException } from './tool-parameter-value-missing.loggable-exception';
export { ContextExternalToolNameAlreadyExistsLoggableException } from './context-external-tool-name-already-exists.loggable-exception';
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ import { ToolParameterDuplicateLoggableException } from './tool-parameter-duplic

describe(ToolParameterDuplicateLoggableException.name, () => {
describe('getLogMessage', () => {
const setup = () => {
const exception: ToolParameterDuplicateLoggableException = new ToolParameterDuplicateLoggableException(
'toolId',
'parameter1'
);

return {
exception,
};
};

it('should return log message', () => {
const exception = new ToolParameterDuplicateLoggableException('parameter1');
const { exception } = setup();

const result = exception.getLogMessage();

Expand All @@ -12,6 +23,7 @@ describe(ToolParameterDuplicateLoggableException.name, () => {
message: 'The parameter is defined multiple times.',
stack: exception.stack,
data: {
toolId: 'toolId',
parameterName: 'parameter1',
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import { ValidationError } from '@shared/common';
import { BusinessError } from '@shared/common';
import { ErrorLogMessage, Loggable, LogMessage, ValidationErrorLogMessage } from '@src/core/logger';
import { HttpStatus } from '@nestjs/common';
import { EntityId } from '@shared/domain/types';

export class ToolParameterDuplicateLoggableException extends ValidationError implements Loggable {
constructor(private readonly parameterName: string) {
super(`tool_param_duplicate: The parameter with name ${parameterName} is defined multiple times.`);
export class ToolParameterDuplicateLoggableException extends BusinessError implements Loggable {
constructor(private readonly toolId: EntityId | undefined, private readonly parameterName: string) {
super(
{
type: 'TOOL_PARAMETER_DUPLICATE',
title: 'Duplicate tool parameter',
defaultMessage: 'The parameter is defined multiple times.',
},
HttpStatus.BAD_REQUEST,
{
toolId,
parameterName,
}
);
}

getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage {
return {
type: 'TOOL_PARAMETER_DUPLICATE',
message: 'The parameter is defined multiple times.',
type: this.type,
message: this.message,
stack: this.stack,
data: {
toolId: this.toolId,
parameterName: this.parameterName,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ describe(ToolParameterRequiredLoggableException.name, () => {
const setup = () => {
const parameter: CustomParameter = customParameterFactory.build();

const exception: ToolParameterRequiredLoggableException = new ToolParameterRequiredLoggableException(parameter);
const exception: ToolParameterRequiredLoggableException = new ToolParameterRequiredLoggableException(
'toolId',
parameter
);

return {
parameter,
Expand All @@ -26,6 +29,7 @@ describe(ToolParameterRequiredLoggableException.name, () => {
stack: exception.stack,
data: {
parameterName: parameter.name,
toolId: 'toolId',
},
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { ValidationError } from '@shared/common';
import { ErrorLogMessage, Loggable, LogMessage, ValidationErrorLogMessage } from '@src/core/logger';
import { HttpStatus } from '@nestjs/common';
import { EntityId } from '@shared/domain/types';
import { BusinessError } from '@shared/common';
import { CustomParameter } from '../custom-parameter.do';

export class ToolParameterRequiredLoggableException extends ValidationError implements Loggable {
constructor(private readonly parameterDeclaration: CustomParameter) {
export class ToolParameterRequiredLoggableException extends BusinessError implements Loggable {
constructor(private readonly toolId: EntityId | undefined, private readonly param: CustomParameter) {
super(
`tool_param_required: The parameter with name ${parameterDeclaration.name} is required but not found in the tool.`
{
type: 'TOOL_PARAMETER_REQUIRED',
title: 'Parameter is required',
defaultMessage: 'The parameter is required, but not found in the tool.',
},
HttpStatus.BAD_REQUEST,
{
param,
toolId,
}
);
}

getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage {
return {
type: 'TOOL_PARAMETER_REQUIRED',
message: 'The parameter is required, but not found in the tool.',
stack: this.stack,
type: this.type,
message: this.message,
data: {
parameterName: this.parameterDeclaration.name,
parameterName: this.param.name,
toolId: this.toolId,
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe(ToolParameterTypeMismatchLoggableException.name, () => {
const parameter: CustomParameter = customParameterFactory.build();

const exception: ToolParameterTypeMismatchLoggableException = new ToolParameterTypeMismatchLoggableException(
'toolId',
parameter
);

Expand All @@ -27,6 +28,7 @@ describe(ToolParameterTypeMismatchLoggableException.name, () => {
message: 'The parameter value has the wrong type.',
stack: exception.stack,
data: {
toolId: 'toolId',
parameterName: parameter.name,
parameterType: parameter.type,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { ValidationError } from '@shared/common';
import { BusinessError } from '@shared/common';
import { ErrorLogMessage, Loggable, LogMessage, ValidationErrorLogMessage } from '@src/core/logger';
import { HttpStatus } from '@nestjs/common';
import { EntityId } from '@shared/domain/types';
import { CustomParameter } from '../custom-parameter.do';

export class ToolParameterTypeMismatchLoggableException extends ValidationError implements Loggable {
constructor(private readonly parameterDeclaration: CustomParameter) {
export class ToolParameterTypeMismatchLoggableException extends BusinessError implements Loggable {
constructor(private readonly toolId: EntityId | undefined, private readonly parameterDeclaration: CustomParameter) {
super(
`tool_param_type_mismatch: The value of parameter with name ${parameterDeclaration.name} should be of type ${parameterDeclaration.type}.`
{
type: 'TOOL_PARAMETER_TYPE_MISMATCH',
title: 'Parameter type mismatch',
defaultMessage: 'The parameter value has the wrong type.',
},
HttpStatus.BAD_REQUEST,
{
toolId,
parameterDeclaration,
}
);
}

getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage {
return {
type: 'TOOL_PARAMETER_TYPE_MISMATCH',
message: 'The parameter value has the wrong type.',
type: this.type,
message: this.message,
stack: this.stack,
data: {
toolId: this.toolId,
parameterName: this.parameterDeclaration.name,
parameterType: this.parameterDeclaration.type,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ describe(ToolParameterUnknownLoggableException.name, () => {
value: 'value1',
});

const exception: ToolParameterUnknownLoggableException = new ToolParameterUnknownLoggableException(parameter);
const exception: ToolParameterUnknownLoggableException = new ToolParameterUnknownLoggableException(
'toolId',
parameter
);

return {
parameter,
Expand All @@ -27,6 +30,7 @@ describe(ToolParameterUnknownLoggableException.name, () => {
message: 'The parameter is not part of this tool.',
stack: exception.stack,
data: {
toolId: 'toolId',
parameterName: parameter.name,
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import { ValidationError } from '@shared/common';
import { BusinessError } from '@shared/common';
import { ErrorLogMessage, Loggable, LogMessage, ValidationErrorLogMessage } from '@src/core/logger';
import { HttpStatus } from '@nestjs/common';
import { EntityId } from '@shared/domain/types';
import { CustomParameterEntry } from '../custom-parameter-entry.do';

export class ToolParameterUnknownLoggableException extends ValidationError implements Loggable {
constructor(private readonly parameterEntry: CustomParameterEntry) {
super(`tool_param_unknown: The parameter with name ${parameterEntry.name} is not part of this tool.`);
export class ToolParameterUnknownLoggableException extends BusinessError implements Loggable {
constructor(private readonly toolId: EntityId | undefined, private readonly parameterEntry: CustomParameterEntry) {
super(
{
type: 'TOOL_PARAMETER_UNKNOWN',
title: 'Tool parameter unknown',
defaultMessage: 'The parameter is not part of this tool.',
},
HttpStatus.BAD_REQUEST,
{
toolId,
parameterEntry,
}
);
}

getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage {
return {
type: 'TOOL_PARAMETER_UNKNOWN',
message: 'The parameter is not part of this tool.',
type: this.type,
message: this.message,
stack: this.stack,
data: {
toolId: this.toolId,
parameterName: this.parameterEntry.name,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe(ToolParameterValueMissingLoggableException.name, () => {
const parameter: CustomParameter = customParameterFactory.build();

const exception: ToolParameterValueMissingLoggableException = new ToolParameterValueMissingLoggableException(
'toolId',
parameter
);

Expand All @@ -28,6 +29,7 @@ describe(ToolParameterValueMissingLoggableException.name, () => {
stack: exception.stack,
data: {
parameterName: parameter.name,
validatableToolId: 'toolId',
},
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import { ValidationError } from '@shared/common';
import { ErrorLogMessage, Loggable, LogMessage, ValidationErrorLogMessage } from '@src/core/logger';
import { BusinessError } from '@shared/common';
import { EntityId } from '@shared/domain/types';
import { HttpStatus } from '@nestjs/common';
import { CustomParameter } from '../custom-parameter.do';

export class ToolParameterValueMissingLoggableException extends ValidationError implements Loggable {
constructor(private readonly parameterDeclaration: CustomParameter) {
super(`tool_param_value_missing: The parameter with name ${parameterDeclaration.name} has no value`);
export class ToolParameterValueMissingLoggableException extends BusinessError implements Loggable {
constructor(private readonly validatableToolId: EntityId | undefined, private readonly parameter: CustomParameter) {
super(
{
type: 'TOOL_PARAMETER_VALUE_MISSING',
title: 'Missing tool parameter value',
defaultMessage: 'The parameter has no value.',
},
HttpStatus.BAD_REQUEST,
{
parameter,
validatableToolId,
}
);
}

getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage {
return {
type: 'TOOL_PARAMETER_VALUE_MISSING',
message: 'The parameter has no value.',
message: this.message,
type: this.type,
stack: this.stack,
data: {
parameterName: this.parameterDeclaration.name,
parameterName: this.parameter.name,
validatableToolId: this.validatableToolId,
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe(ToolParameterValueRegexLoggableException.name, () => {
const parameter: CustomParameter = customParameterFactory.build();

const exception: ToolParameterValueRegexLoggableException = new ToolParameterValueRegexLoggableException(
'toolId',
parameter
);

Expand All @@ -27,6 +28,7 @@ describe(ToolParameterValueRegexLoggableException.name, () => {
message: 'The parameter value does not fit the regex.',
stack: exception.stack,
data: {
toolId: 'toolId',
parameterName: parameter.name,
},
});
Expand Down
Loading

0 comments on commit 8647f31

Please sign in to comment.