Skip to content

Commit

Permalink
feat: removing useless error
Browse files Browse the repository at this point in the history
  • Loading branch information
NarwhalChen committed Jan 13, 2025
1 parent 2d2bb8d commit cf221cb
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 445 deletions.
46 changes: 46 additions & 0 deletions backend/src/build-system/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Error thrown when a required file is not found.
* Typically used in file handling operations to indicate missing files.
*/
export class FileNotFoundError extends Error {
constructor(message: string) {
Expand All @@ -10,6 +11,7 @@ export class FileNotFoundError extends Error {

/**
* Error thrown when there is an issue modifying a file.
* Indicates a failure in updating or altering file content.
*/
export class FileModificationError extends Error {
constructor(message: string) {
Expand All @@ -18,8 +20,20 @@ export class FileModificationError extends Error {
}
}

/**
* Error thrown when the model service is unavailable.
* Used to signal that the underlying model cannot be reached or is down.
*/
export class ModelUnavailableError extends Error {
constructor(message: string) {
super(message);
this.name = 'ModelUnavailableError';
}
}

/**
* Error thrown when parsing a response fails.
* Indicates that the system could not properly interpret the response data.
*/
export class ResponseParsingError extends Error {
constructor(message: string) {
Expand All @@ -28,55 +42,87 @@ export class ResponseParsingError extends Error {
}
}

/**
* Error thrown when the expected tags in a response are missing or invalid.
* Typically occurs during content generation or parsing steps.
*/
export class ResponseTagError extends Error {
constructor(message: string) {
super(message);
this.name = 'ResponseTagError';
}
}

/**
* Error thrown when a timeout occurs while interacting with the model service.
* Used to handle scenarios where requests to the model exceed allowed time limits.
*/
export class ModelTimeoutError extends Error {
constructor(message: string) {
super(message);
this.name = 'ModelTimeoutError';
}
}

/**
* Error thrown when the model service is temporarily unavailable.
* Indicates transient issues, such as server overload or maintenance downtime.
*/
export class TemporaryServiceUnavailableError extends Error {
constructor(message: string) {
super(message);
this.name = 'TemporaryServiceUnavailableError';
}
}

/**
* Error thrown when the rate limit for the model service is exceeded.
* Used to signal that too many requests have been sent within a given time frame.
*/
export class RateLimitExceededError extends Error {
constructor(message: string) {
super(message);
this.name = 'RateLimitExceededError';
}
}

/**
* Error thrown when required configuration is missing or invalid.
* Indicates issues with system setup or missing configuration parameters.
*/
export class MissingConfigurationError extends Error {
constructor(message: string) {
super(message);
this.name = 'MissingConfigurationError';
}
}

/**
* Error thrown when a provided parameter is invalid.
* Used to signal issues with function arguments or configurations.
*/
export class InvalidParameterError extends Error {
constructor(message: string) {
super(message);
this.name = 'InvalidParameterError';
}
}

/**
* Error thrown when a failure occurs while writing to a file.
* Indicates issues such as insufficient permissions or disk errors.
*/
export class FileWriteError extends Error {
constructor(message: string) {
super(message);
this.name = 'FileWriteError';
}
}

/**
* Error thrown when a general parsing failure occurs.
* Can be used to handle issues in various parsing contexts.
*/
export class ParsingError extends Error {
constructor(message: string) {
super(message);
Expand Down
60 changes: 6 additions & 54 deletions backend/src/build-system/handlers/backend/code-generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { saveGeneratedCode } from 'src/build-system/utils/files';
import * as path from 'path';
import { formatResponse } from 'src/build-system/utils/strings';
import {
ModelTimeoutError,
TemporaryServiceUnavailableError,
RateLimitExceededError,
MissingConfigurationError,
InvalidParameterError,
FileWriteError,
Expand Down Expand Up @@ -70,18 +67,14 @@ export class BackendCodeHandler implements BuildHandler<string> {
dependencyFile,
);

let modelResponse: string;
let modelResponse;
try {
modelResponse = await this.callModel(context, backendCodePrompt);
modelResponse = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: backendCodePrompt, role: 'system' }],
});
} catch (error) {
if (
error instanceof ModelTimeoutError ||
error instanceof TemporaryServiceUnavailableError ||
error instanceof RateLimitExceededError
) {
throw error; // Retryable errors will be handled externally.
}
throw new Error(`Unexpected model error: ${error.message}`);
throw error;
}

let generatedCode: string;
Expand Down Expand Up @@ -117,45 +110,4 @@ export class BackendCodeHandler implements BuildHandler<string> {
};
}

/**
* Calls the language model to generate backend code.
* @param context The builder context.
* @param prompt The generated prompt.
*/
private async callModel(
context: BuilderContext,
prompt: string,
): Promise<string> {
try {
const modelResponse = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: prompt, role: 'system' }],
});

if (!modelResponse) {
throw new ModelTimeoutError(
'The model did not respond within the expected time.',
);
}

return modelResponse;
} catch (error) {
if (error.message.includes('timeout')) {
throw new ModelTimeoutError(
'Timeout occurred while communicating with the model.',
);
}
if (error.message.includes('service unavailable')) {
throw new TemporaryServiceUnavailableError(
'Model service is temporarily unavailable.',
);
}
if (error.message.includes('rate limit')) {
throw new RateLimitExceededError(
'Rate limit exceeded for model service.',
);
}
throw new Error(`Unexpected model error: ${error.message}`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class BackendFileReviewHandler implements BuildHandler<string> {
}
this.logger.debug(`Found files: ${files.join(', ')}`);
} catch (error) {
this.handleFileSystemError(error);
throw error;
}

const filePrompt = prompts.identifyBackendFilesToModify(
Expand All @@ -70,7 +70,7 @@ export class BackendFileReviewHandler implements BuildHandler<string> {
messages: [{ content: filePrompt, role: 'system' }],
});
} catch (error) {
this.handleModelError(error);
throw error;
}

const filesToModify = this.parseFileIdentificationResponse(modelResponse);
Expand Down Expand Up @@ -106,7 +106,7 @@ export class BackendFileReviewHandler implements BuildHandler<string> {
await fs.writeFile(filePath, newContent, 'utf-8');
this.logger.log(`Successfully modified ${fileName}`);
} catch (error) {
this.handleFileProcessingError(fileName, error);
throw error;
}
}

Expand Down Expand Up @@ -136,50 +136,4 @@ export class BackendFileReviewHandler implements BuildHandler<string> {
);
}
}

/**
* Handles file system errors.
*/
private handleFileSystemError(error: any): never {
this.logger.error('File system error encountered:', error);
throw new FileNotFoundError(
`File system operation failed: ${error.message}`,
);
}

/**
* Handles model-related errors.
*/
private handleModelError(error: any): never {
if (
error instanceof ModelTimeoutError ||
error instanceof TemporaryServiceUnavailableError ||
error instanceof RateLimitExceededError
) {
this.logger.warn(`Retryable model error: ${error.message}`);
throw error;
}
this.logger.error('Non-retryable model error encountered:', error);
throw new ResponseParsingError(`Model error: ${error.message}`);
}

/**
* Handles errors during file processing.
*/
private handleFileProcessingError(fileName: string, error: any): never {
if (
error instanceof ModelTimeoutError ||
error instanceof TemporaryServiceUnavailableError ||
error instanceof RateLimitExceededError
) {
this.logger.warn(
`Retryable error for file ${fileName}: ${error.message}`,
);
throw error;
}
this.logger.error(`Non-retryable error for file ${fileName}:`, error);
throw new FileModificationError(
`Error processing file ${fileName}: ${error.message}`,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ModelTimeoutError,
TemporaryServiceUnavailableError,
RateLimitExceededError,
ModelUnavailableError,
} from 'src/build-system/errors';

type BackendRequirementResult = {
Expand Down Expand Up @@ -67,13 +68,21 @@ export class BackendRequirementHandler

let backendOverview: string;
try {
backendOverview = await this.callModel(context, overviewPrompt);
if (!backendOverview || backendOverview.trim() === '') {
backendOverview = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: overviewPrompt, role: 'system' }],
});

if (!backendOverview) {
throw new ModelTimeoutError(
'The model did not respond within the expected time.',
);
}
if (backendOverview.trim() === '') {
throw new ResponseParsingError('Generated backend overview is empty.');
}
} catch (error) {
this.logger.error('Error during backend overview generation:', error);
throw error; // Pass error to upper-level handler
throw error;
}

// Return generated data
Expand All @@ -91,45 +100,4 @@ export class BackendRequirementHandler
};
}

/**
* Calls the language model to generate backend overview.
* @param context The builder context.
* @param prompt The generated prompt.
*/
private async callModel(
context: BuilderContext,
prompt: string,
): Promise<string> {
try {
const modelResponse = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: prompt, role: 'system' }],
});

if (!modelResponse) {
throw new ModelTimeoutError(
'The model did not respond within the expected time.',
);
}

return modelResponse;
} catch (error) {
if (error.message.includes('timeout')) {
throw new ModelTimeoutError(
'Timeout occurred while communicating with the model.',
);
}
if (error.message.includes('service unavailable')) {
throw new TemporaryServiceUnavailableError(
'Model service is temporarily unavailable.',
);
}
if (error.message.includes('rate limit')) {
throw new RateLimitExceededError(
'Rate limit exceeded for model service.',
);
}
throw new Error(`Unexpected model error: ${error.message}`);
}
}
}
Loading

0 comments on commit cf221cb

Please sign in to comment.