/**
* Enhanced Error Handling System for Logic-Thinking MCP Server
* Part of Phase 4b Implementation
*/
import { LogicalSystem, Operation } from './types.js';
// Error type definitions
export type ErrorType = 'SyntaxError' | 'ValidationError' | 'CommandError' | 'MathematicalError' | 'UnsupportedOperation';
// Enhanced error interface
export interface EnhancedLogicError {
status: 'error';
code: string;
message: string;
details: {
errorType: ErrorType;
system: LogicalSystem;
location?: string;
suggestion?: string;
example?: string;
originalInput?: string;
};
}
/**
* Creates a syntax error for parsing issues
*/
export function createSyntaxError(
system: LogicalSystem,
message: string,
location: string,
suggestion: string,
example: string,
originalInput?: string
): EnhancedLogicError {
return {
status: 'error',
code: `SYNTAX_${system.toUpperCase()}_001`,
message,
details: {
errorType: 'SyntaxError',
system,
location,
suggestion,
example,
originalInput
}
};
}
/**
* Creates a validation error for logical rule violations
*/
export function createValidationError(
system: LogicalSystem,
message: string,
location: string,
suggestion: string,
example: string,
originalInput?: string
): EnhancedLogicError {
return {
status: 'error',
code: `VALID_${system.toUpperCase()}_001`,
message,
details: {
errorType: 'ValidationError',
system,
location,
suggestion,
example,
originalInput
}
};
}
/**
* Creates a command error for incorrect command usage
*/
export function createCommandError(
system: LogicalSystem,
message: string,
suggestion: string,
example: string,
originalInput?: string
): EnhancedLogicError {
return {
status: 'error',
code: `CMD_${system.toUpperCase()}_001`,
message,
details: {
errorType: 'CommandError',
system,
suggestion,
example,
originalInput
}
};
}
/**
* Creates a mathematical error for math-specific issues
*/
export function createMathematicalError(
message: string,
location: string,
suggestion: string,
example: string,
originalInput?: string
): EnhancedLogicError {
return {
status: 'error',
code: 'MATH_001',
message,
details: {
errorType: 'MathematicalError',
system: 'mathematical',
location,
suggestion,
example,
originalInput
}
};
}
/**
* Creates an unsupported operation error
*/
export function createUnsupportedOperationError(
system: LogicalSystem,
operation: Operation,
suggestion?: string,
example?: string
): EnhancedLogicError {
return {
status: 'error',
code: `UNSUPPORTED_${system.toUpperCase()}_${operation.toUpperCase()}`,
message: `Operation '${operation}' is not supported for the ${system} logical system.`,
details: {
errorType: 'UnsupportedOperation',
system,
suggestion: suggestion || `Try using a different operation or logical system.`,
example: example || `Use 'validate' instead of '${operation}' for ${system} logic.`
}
};
}
// Common error message templates
export const errorTemplates = {
// Syntax error templates
syntax: {
propositional: {
invalidSymbol: (symbol: string) =>
`Invalid symbol '${symbol}' in propositional formula.`,
missingOperator:
"Missing operator between expressions.",
unbalancedParentheses:
"Unbalanced parentheses in the formula.",
invalidVariable: (variable: string) =>
`Invalid variable name '${variable}'. Variable names must follow proper format.`
},
predicate: {
invalidQuantifier: (quantifier: string) =>
`Invalid quantifier '${quantifier}'. Use 'forall', 'exists', '∀', or '∃'.`,
missingVariable:
"Missing variable after quantifier.",
invalidPredicate: (predicate: string) =>
`Invalid predicate '${predicate}'. Predicates must follow proper format.`
},
syllogistic: {
invalidStatement:
"Invalid syllogistic statement. Must be in form 'All/Some/No S are P'.",
missingTerms:
"Missing subject or predicate in syllogistic statement."
},
mathematical: {
invalidExpression:
"Invalid mathematical expression.",
unbalancedEquation:
"Unbalanced equation. Check for missing terms or operators.",
invalidSequence:
"Invalid sequence format. Use comma-separated numbers like '1, 2, 3, 4'."
}
},
// Validation error templates
validation: {
propositional: {
invalidArgument:
"Invalid propositional argument structure.",
tautology:
"The formula is a tautology (always true).",
contradiction:
"The formula is a contradiction (always false)."
},
predicate: {
scopeError:
"Invalid quantifier scope.",
domainError:
"Invalid domain for predicate application."
},
syllogistic: {
invalidMood:
"Invalid syllogistic mood.",
invalidFigure:
"Invalid syllogistic figure.",
fallacyOfExclusion:
"Fallacy of exclusion: both terms are negative in the conclusion."
},
mathematical: {
divisionByZero:
"Division by zero error.",
insufficientData:
"Insufficient data to determine the pattern.",
noSolution:
"The equation has no solution in the given domain."
}
},
// Command error templates
command: {
invalidParameter: (param: string) =>
`Invalid parameter '${param}'.`,
missingParameter: (param: string) =>
`Missing required parameter '${param}'.`,
invalidFormat:
"Invalid input format. Use 'natural', 'symbolic', or 'mixed'."
}
};
// Error suggestion templates
export const errorSuggestions = {
syntax: {
propositional: {
invalidSymbol: (symbol: string) =>
`Replace '${symbol}' with a valid operator like '∧' (and), '∨' (or), '→' (implies), or '↔' (if and only if).`,
missingOperator:
"Insert an operator between expressions, such as '∧' (and) or '∨' (or).",
unbalancedParentheses:
"Ensure each opening parenthesis '(' has a matching closing parenthesis ')'.",
invalidVariable:
"Use single letters (A-Z, a-z) for variables in propositional logic."
},
predicate: {
invalidQuantifier:
"Use '∀' or 'forall' for universal quantifier, '∃' or 'exists' for existential quantifier.",
missingVariable:
"Specify a variable after the quantifier, e.g., '∀x' or 'exists y'.",
invalidPredicate:
"Use predicates in the form P(x) where P is the predicate name and x is a variable."
},
syllogistic: {
invalidStatement:
"Use standard forms: 'All S are P', 'Some S are P', 'No S are P', or 'Some S are not P'.",
missingTerms:
"Ensure each statement has both a subject and a predicate."
},
mathematical: {
invalidExpression:
"Ensure proper mathematical notation with valid operators and operands.",
unbalancedEquation:
"Check that your equation has an equal number of terms on both sides of the equals sign.",
invalidSequence:
"Format sequences as comma-separated numbers, e.g., '1, 2, 3, 4'."
}
},
validation: {
propositional: {
invalidArgument:
"Ensure your premises and conclusion form a valid logical structure.",
tautology:
"Consider simplifying your formula since it's always true.",
contradiction:
"Revise your formula since it cannot be satisfied."
},
predicate: {
scopeError:
"Check the scope of your quantifiers and ensure proper nesting.",
domainError:
"Ensure your predicates are applied to terms of the correct type."
},
syllogistic: {
invalidMood:
"Check the combination of statement types (A, E, I, O) in your syllogism.",
invalidFigure:
"Verify the arrangement of terms in your premises.",
fallacyOfExclusion:
"Revise your argument to avoid having both terms negative in the conclusion."
},
mathematical: {
divisionByZero:
"Modify your expression to avoid dividing by zero.",
insufficientData:
"Provide more terms in the sequence to detect a pattern.",
noSolution:
"Check if your constraints are too restrictive or contradictory."
}
},
command: {
invalidParameter:
"Refer to the documentation for valid parameter values.",
missingParameter:
"Include all required parameters for the operation.",
invalidFormat:
"Use 'natural' for natural language, 'symbolic' for formal notation, or 'mixed' for a combination."
}
};
// Example templates
export const exampleTemplates = {
syntax: {
propositional: {
invalidSymbol:
"P ∧ (Q ∨ R) → S",
missingOperator:
"P ∧ Q → R",
unbalancedParentheses:
"P ∧ (Q ∨ R) → S",
invalidVariable:
"P ∧ Q → R"
},
predicate: {
invalidQuantifier:
"∀x (P(x) → Q(x))",
missingVariable:
"∀x ∃y (P(x, y))",
invalidPredicate:
"P(x) ∧ Q(x, y)"
},
syllogistic: {
invalidStatement:
"All humans are mortal. Socrates is a human. Therefore, Socrates is mortal.",
missingTerms:
"All dogs are mammals. All mammals are animals. Therefore, all dogs are animals."
},
mathematical: {
invalidExpression:
"x + 5 = 10",
unbalancedEquation:
"2x + 3 = 5x - 7",
invalidSequence:
"1, 2, 4, 8, 16"
}
},
validation: {
propositional: {
invalidArgument:
"Premise: P → Q\nPremise: Q → R\nConclusion: P → R",
tautology:
"P ∨ ¬P",
contradiction:
"P ∧ ¬P"
},
predicate: {
scopeError:
"∀x ∃y (P(x, y) → Q(x))",
domainError:
"∀x (Human(x) → Mortal(x))"
},
syllogistic: {
invalidMood:
"All M are P. All S are M. Therefore, all S are P.",
invalidFigure:
"All P are M. All M are S. Therefore, all S are P.",
fallacyOfExclusion:
"No M are P. All S are M. Therefore, no S are P."
},
mathematical: {
divisionByZero:
"x = 10 / (y - 3) where y = 3",
insufficientData:
"1, 2, 4, 8, 16, ...",
noSolution:
"x + 5 = x + 6"
}
},
command: {
invalidParameter:
"validate --system=propositional --input=\"P ∧ Q → R\"",
missingParameter:
"visualize --system=syllogistic --input=\"All humans are mortal. Socrates is a human. Therefore, Socrates is mortal.\"",
invalidFormat:
"formalize --system=predicate --format=natural --input=\"For all x, if x is a human, then x is mortal.\""
}
};