Skip to content

Commit 6923ead

Browse files
committed
Replace ApolloErrorProcessor class with processApolloError function
Allows error handling to be configured through an options object instead of by re-implementing the class.
1 parent 9b0d0e0 commit 6923ead

File tree

6 files changed

+245
-255
lines changed

6 files changed

+245
-255
lines changed

src/error/ApolloErrorHandlerResult.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,38 @@ import {
1010
} from './types';
1111

1212
export interface ApolloErrorHandlerResultInterface {
13-
processedErrors: ProcessedApolloError[];
13+
allErrors: ProcessedApolloError[];
1414
validationRuleViolations?: ValidationRuleViolation[];
1515
}
1616

1717
export class ApolloErrorHandlerResult implements ApolloErrorHandlerResultInterface {
18+
public readonly allErrors: ProcessedApolloError[];
19+
1820
public constructor(
19-
public readonly processedErrors: ProcessedApolloError[],
21+
public readonly unhandledErrors: ProcessedApolloError[],
2022
public readonly handledErrors: ProcessedApolloError[],
21-
) {}
23+
) {
24+
this.allErrors = [...unhandledErrors, ...handledErrors];
25+
}
2226

2327
public get networkErrors(): NetworkError[] {
24-
return this.processedErrors.filter((e): e is NetworkError => e.type === ApolloErrorType.NETWORK_ERROR);
28+
return this.allErrors.filter((e): e is NetworkError => e.type === ApolloErrorType.NETWORK_ERROR);
2529
}
2630

2731
public get serverErrors(): ServerError[] {
28-
return this.processedErrors.filter((e): e is ServerError => e.type === ApolloErrorType.SERVER_ERROR);
32+
return this.allErrors.filter((e): e is ServerError => e.type === ApolloErrorType.SERVER_ERROR);
2933
}
3034

3135
public get unauthorizedErrors(): UnauthorizedError[] {
32-
return this.processedErrors.filter((e): e is UnauthorizedError => e.type === ApolloErrorType.UNAUTHORIZED_ERROR);
36+
return this.allErrors.filter((e): e is UnauthorizedError => e.type === ApolloErrorType.UNAUTHORIZED_ERROR);
3337
}
3438

3539
public get userInputErrors(): UserInputError[] {
36-
return this.processedErrors.filter((e): e is UserInputError => e.type === ApolloErrorType.BAD_USER_INPUT);
40+
return this.allErrors.filter((e): e is UserInputError => e.type === ApolloErrorType.BAD_USER_INPUT);
3741
}
3842

3943
public get inputValidationErrors(): InputValidationError[] {
40-
return this.processedErrors.filter(
41-
(e): e is InputValidationError => e.type === ApolloErrorType.INPUT_VALIDATION_ERROR,
42-
);
44+
return this.allErrors.filter((e): e is InputValidationError => e.type === ApolloErrorType.INPUT_VALIDATION_ERROR);
4345
}
4446

4547
public get validationRuleViolations(): ValidationRuleViolation[] {

src/error/ApolloErrorProcessor.ts

Lines changed: 0 additions & 236 deletions
This file was deleted.

src/error/handleApolloError.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { ApolloErrorProcessor } from './ApolloErrorProcessor';
1+
import { processApolloError } from './processApolloError';
22
import { ApolloOperationContext } from '../types';
33
import { Vue } from 'vue/types/vue';
44
import { ApolloError, ApolloOperationErrorHandlerFunction } from './types';
5-
import { ApolloErrorHandlerResultInterface } from './ApolloErrorHandlerResult';
5+
import { ApolloErrorHandlerResult, ApolloErrorHandlerResultInterface } from './ApolloErrorHandlerResult';
66

77
/**
88
* This is a simple example of an error handler function. You can copy this and implement your own in your application.
@@ -12,11 +12,22 @@ export const handleApolloError: ApolloOperationErrorHandlerFunction<ApolloError,
1212
app: Vue,
1313
context?: ApolloOperationContext,
1414
): ApolloErrorHandlerResultInterface => {
15-
const processor = new ApolloErrorProcessor(error, app, context ?? {});
15+
const { unhandledErrors, handledErrors } = processApolloError(error, {
16+
app,
17+
context,
18+
// Example of a handler function for a particular type of error:
19+
onUnauthorizedError: error => {
20+
console.warn('Unauthorized! Logging you out...', error);
21+
//logout();
1622

17-
processor.showErrorNotifications();
23+
// Returning true indicates to the processor that we've handled this error
24+
return true;
25+
},
26+
});
1827

19-
return {
20-
processedErrors: processor.processedErrors,
21-
};
28+
unhandledErrors.forEach(error => {
29+
console.error(`${error.type}: ${error.message}`, error.error);
30+
});
31+
32+
return new ApolloErrorHandlerResult(unhandledErrors, handledErrors);
2233
};

src/error/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
*/
44

55
export * from './ApolloErrorHandlerResult';
6-
export * from './ApolloErrorProcessor';
76
export * from './handleApolloError';
7+
export * from './processApolloError';
88
export * from './types';

0 commit comments

Comments
 (0)