Skip to content

Commit ae4cced

Browse files
authored
Merge pull request #101 from madscience/error-results
Make error handling easier to configure
2 parents e4950fd + 05ec76b commit ae4cced

File tree

12 files changed

+423
-375
lines changed

12 files changed

+423
-375
lines changed

src/ApolloErrorProcessor.ts

Lines changed: 0 additions & 230 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
ApolloErrorType,
3+
InputValidationError,
4+
NetworkError,
5+
ProcessedApolloError,
6+
ServerError,
7+
UnauthorizedError,
8+
UserInputError,
9+
ValidationRuleViolation,
10+
} from './types';
11+
12+
export interface ApolloErrorHandlerResultInterface {
13+
allErrors: ProcessedApolloError[];
14+
validationRuleViolations?: ValidationRuleViolation[];
15+
}
16+
17+
export class ApolloErrorHandlerResult implements ApolloErrorHandlerResultInterface {
18+
public readonly allErrors: ProcessedApolloError[];
19+
20+
public constructor(
21+
public readonly unhandledErrors: ProcessedApolloError[],
22+
public readonly handledErrors: ProcessedApolloError[],
23+
) {
24+
this.allErrors = [...unhandledErrors, ...handledErrors];
25+
}
26+
27+
public get networkErrors(): NetworkError[] {
28+
return this.allErrors.filter((e): e is NetworkError => e.type === ApolloErrorType.NETWORK_ERROR);
29+
}
30+
31+
public get serverErrors(): ServerError[] {
32+
return this.allErrors.filter((e): e is ServerError => e.type === ApolloErrorType.SERVER_ERROR);
33+
}
34+
35+
public get unauthorizedErrors(): UnauthorizedError[] {
36+
return this.allErrors.filter((e): e is UnauthorizedError => e.type === ApolloErrorType.UNAUTHORIZED_ERROR);
37+
}
38+
39+
public get userInputErrors(): UserInputError[] {
40+
return this.allErrors.filter((e): e is UserInputError => e.type === ApolloErrorType.BAD_USER_INPUT);
41+
}
42+
43+
public get inputValidationErrors(): InputValidationError[] {
44+
return this.allErrors.filter((e): e is InputValidationError => e.type === ApolloErrorType.INPUT_VALIDATION_ERROR);
45+
}
46+
47+
public get validationRuleViolations(): ValidationRuleViolation[] {
48+
return this.inputValidationErrors.flatMap(e => e.violations);
49+
}
50+
}

src/error/handleApolloError.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { processApolloError } from './processApolloError';
2+
import { ApolloOperationContext } from '../types';
3+
import { Vue } from 'vue/types/vue';
4+
import { ApolloError, ApolloOperationErrorHandlerFunction } from './types';
5+
import { ApolloErrorHandlerResult } from './ApolloErrorHandlerResult';
6+
7+
/**
8+
* This is a simple example of an error handler function. You can copy this and implement your own in your application.
9+
*/
10+
export const handleApolloError: ApolloOperationErrorHandlerFunction<
11+
ApolloError,
12+
Vue,
13+
ApolloOperationContext,
14+
ApolloErrorHandlerResult
15+
> = (error: ApolloError, app: Vue, context?: ApolloOperationContext): ApolloErrorHandlerResult => {
16+
const { unhandledErrors, handledErrors } = processApolloError(error, {
17+
app,
18+
context,
19+
// Example of a handler function for a particular type of error:
20+
onUnauthorizedError: error => {
21+
console.warn('Unauthorized! Logging you out...', error);
22+
//logout();
23+
24+
// Returning true indicates to the processor that we've handled this error
25+
return true;
26+
},
27+
});
28+
29+
unhandledErrors.forEach(error => {
30+
console.error(`${error.type}: ${error.message}`, error.error);
31+
});
32+
33+
return new ApolloErrorHandlerResult(unhandledErrors, handledErrors);
34+
};

src/error/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @file Automatically generated by barrelsby.
3+
*/
4+
5+
export * from './ApolloErrorHandlerResult';
6+
export * from './handleApolloError';
7+
export * from './processApolloError';
8+
export * from './types';

0 commit comments

Comments
 (0)