New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@keepfy/error-extractor

Package Overview
Dependencies
Maintainers
2
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@keepfy/error-extractor - npm Package Compare versions

Comparing version

to
1.2.0

lib/suggestions.d.ts

7

lib/error.d.ts

@@ -1,3 +0,4 @@

import { ErrorMessage, TExtractMessageFromError } from './types';
export declare const extractFromApollo: TExtractMessageFromError;
export declare const extractFrom: ({ graphQLErrors, networkError }: import("apollo-link-error").ErrorResponse) => ErrorMessage;
import { AllErrorTypes, ExtractMessageFromError, GraphQLErrors } from './types';
export declare const fromGraphQLError: (graphQLErrors: GraphQLErrors[]) => AllErrorTypes;
export declare const fromApollo: ExtractMessageFromError;
export declare const fromResponse: ({ graphQLErrors, networkError }: import("apollo-link-error").ErrorResponse) => AllErrorTypes;

@@ -1,76 +0,41 @@

const keepfyOfflineMessage = {
type: 'SERVICE_OFFLINE',
title: 'Keepfy fora do ar',
text: 'O serviço pode estar enfrentando problemas no momento'
};
const keepfyConnectionFailed = {
type: 'CONNECTION_FAILED',
title: 'Erro de conexão',
text: 'Não foi possível conectar-se ao keepfy'
};
const keepfyUnknownField = {
type: 'SCHEMA_UNKNOWN_FIELD',
title: 'Erro de conexão',
text: 'Parece que seu app pode estar desatualizado'
};
const keepfyUnknownError = {
type: 'UNKNOWN_ERROR',
title: 'Erro desconhecido',
text: 'Erro não identificado, contate o administrador'
};
const keepfyNeedEmailConfirmation = {
type: 'EMAIL_NOT_CONFIRMED',
title: 'Verificação necessária',
text: 'Verifique seu e-mail para acessar o sistema'
};
const backendMessagesMap = {
AUTHENTICATION_FAILED: {
type: 'AUTHENTICATION_FAILED',
title: 'Ops!',
text: 'Autenticação de usuário falhou!'
},
FORBIDDEN: {
type: 'FORBIDDEN',
title: 'Não autorizado',
text: 'Você não possui permissão para realizar esta ação.'
}
};
const extractFromMessage = message => {
const typeFromMessage = message => {
if (message === null) {
return keepfyUnknownError;
return 'UNKNOWN_ERROR';
}
// When response body is empty
if (message.includes('JSON parse error: Unexpected identifier')) {
return keepfyUnknownError;
return 'UNKNOWN_ERROR';
}
// When we receive a xml instead of a response
if (message.includes('Unexpected token')) {
return keepfyOfflineMessage;
return 'SERVICE_OFFLINE';
}
if (message.includes('Network request failed')) {
return keepfyConnectionFailed;
return 'CONNECTION_FAILED';
}
// When graphql finds a wrong field sent
if (message.includes('Unknown argument')) {
return keepfyUnknownField;
return 'SCHEMA_UNKNOWN_FIELD';
}
// Not sure if we still get this one
if (message.includes('Access denied')) {
return backendMessagesMap['FORBIDDEN'];
return 'FORBIDDEN';
}
if (message.includes('Key (email)') && message.includes('already exists')) {
return 'EMAIL_ALREADY_EXISTS';
}
if (message.includes('Verifique seu e-mail para acessar o sistema')) {
return keepfyNeedEmailConfirmation;
return 'EMAIL_NOT_CONFIRMED';
}
return {
type: 'UNKNOWN_ERROR',
title: 'Erro não identificado',
text: message
};
return 'UNKNOWN_ERROR';
};
const extractFromGraphQLError = (graphQLErrors) => {
const fallbackTypes = [
'BUSINESS_ERROR',
'UNKNOWN_ERROR'
];
export const fromGraphQLError = (graphQLErrors) => {
// Should we ignore other errors?
const [error] = graphQLErrors;
const extractedBackend = backendMessagesMap[error.code];
// Fallback to message include calls
if (!extractedBackend) {
if (fallbackTypes.includes(error.code)) {
const properties = error.properties;

@@ -80,28 +45,23 @@ const message = properties && properties.message

: error.message; // old format
const extracted = extractFromMessage(message);
/*
* We cannot trust BUSINESS_ERROR right now
* so we use the text mapped type + the
* backend sent message
* so we use the text mapped type
*/
if (error.code === 'BUSINESS_ERROR') {
return {
...extracted,
text: message
};
return typeFromMessage(message);
}
return extracted;
return error.code;
}
return extractedBackend;
return error.code;
};
export const extractFromApollo = error => {
export const fromApollo = error => {
if (error.networkError) {
return extractFromMessage(error.networkError.message);
return typeFromMessage(error.networkError.message);
}
if ((error.graphQLErrors || []).length) {
return extractFromGraphQLError(error.graphQLErrors);
return fromGraphQLError(error.graphQLErrors);
}
return extractFromMessage(error.message);
return typeFromMessage(error.message);
};
export const extractFrom = ({ graphQLErrors, networkError }) => extractFromApollo({
export const fromResponse = ({ graphQLErrors, networkError }) => fromApollo({
graphQLErrors: graphQLErrors || [],

@@ -108,0 +68,0 @@ networkError: networkError || null,

@@ -1,2 +0,3 @@

export { extractFromApollo, extractFrom } from './error';
export { fromApollo, fromResponse } from './error';
export { getSuggestion, suggestionFromGraphQLError } from './suggestions';
export { forwardToSentry } from './sentry-handler';

@@ -1,2 +0,3 @@

export { extractFromApollo, extractFrom } from './error';
export { fromApollo, fromResponse } from './error';
export { getSuggestion, suggestionFromGraphQLError } from './suggestions';
export { forwardToSentry } from './sentry-handler';
import { ErrorResponse } from 'apollo-link-error';
import { ErrorMessage, SentryAdapter } from './types';
import { AllErrorTypes, SentryAdapter } from './types';
export declare const forwardToSentry: (sentry: SentryAdapter) => {
captureIfNeeded: (mappedError: ErrorMessage, error: ErrorResponse) => void;
captureIfNeeded: (type: AllErrorTypes, error: ErrorResponse) => void;
};

@@ -44,4 +44,4 @@ const typeToCapture = [

};
const captureIfNeeded = (sentry) => (mappedError, error) => {
if (!typeToCapture.includes(mappedError.type)) {
const captureIfNeeded = (sentry) => (type, error) => {
if (!typeToCapture.includes(type)) {
return;

@@ -56,3 +56,3 @@ }

'Operation name': operationName,
'Mapped type': mappedError.type
'Mapped type': type
});

@@ -59,0 +59,0 @@ // We take the first one available (network or gql)

import { ErrorResponse } from 'apollo-link-error';
import { ApolloError } from 'apollo-client';
export declare type ClientErrorTypes = 'SERVICE_OFFLINE' | 'CONNECTION_FAILED' | 'UNKNOWN_ERROR' | 'EMAIL_NOT_CONFIRMED' | 'SCHEMA_UNKNOWN_FIELD';
export declare type ClientErrorTypes = 'SERVICE_OFFLINE' | 'CONNECTION_FAILED' | 'UNKNOWN_ERROR' | 'EMAIL_NOT_CONFIRMED' | 'EMAIL_ALREADY_EXISTS' | 'SCHEMA_UNKNOWN_FIELD';
export declare type BackEndErrorTypes = 'AUTHENTICATION_FAILED' | 'FORBIDDEN' | 'BUSINESS_ERROR';
export declare type BackendMessagesMap = {
[key in BackEndErrorTypes]?: ErrorMessage & {
export declare type AllErrorTypes = ClientErrorTypes | BackEndErrorTypes;
export declare type MessageSuggestion = {
type: AllErrorTypes;
title: string;
message: string;
};
export declare type SuggestionsMap = {
[key in AllErrorTypes]: MessageSuggestion & {
type: key;
};
};
export declare type ErrorMessage = {
type: ClientErrorTypes | BackEndErrorTypes;
title: string;
text: string;
};
export declare type TGraphQLErrors = {
export declare type GraphQLErrors = {
code: BackEndErrorTypes;

@@ -21,4 +22,4 @@ message: string;

};
export declare type TExtractMessageFromError = (error: ApolloError) => ErrorMessage;
export declare type TExtractFromError = (message: string | null) => ErrorMessage;
export declare type ExtractMessageFromError = (error: ApolloError) => AllErrorTypes;
export declare type ExtractFromError = (message: string | null) => AllErrorTypes;
export declare type LinkErrorResponse = ErrorResponse;

@@ -25,0 +26,0 @@ export declare type Severity = 'fatal' | 'error' | 'warning' | 'log' | 'info' | 'debug' | 'critical';

{
"name": "@keepfy/error-extractor",
"version": "1.1.3",
"version": "1.2.0",
"description": "An error extractor package for keepfy frontend apps",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -26,3 +26,2 @@ # Keepfy error extractor

// ...

@@ -32,5 +31,5 @@ new ApolloClient({

onError: (error) => {
const extracted = KeepfyErrorExtractor.extractFrom(error)
const extracted = KeepfyErrorExtractor.fromResponse(error)
if(extracted.type === 'INVALID_TOKEN'){
if(extracted === 'INVALID_TOKEN'){
// do redirect to login emit

@@ -61,7 +60,7 @@ }

onError: (error) => {
const extracted = KeepfyErrorExtractor.extractFrom(error)
const extracted = KeepfyErrorExtractor.fromResponse(error)
sentryForward.captureIfNeeded(extracted, error)
if(extracted.type === 'INVALID_TOKEN'){
if(extracted === 'INVALID_TOKEN'){
// do redirect to login emit

@@ -91,1 +90,25 @@ }

just write your adapter for it.
### Suggestions
The package offers message suggestions (since not everyone will the strings) separated
you can easily get a suggestion like this:
```typescript
import * as KeepfyErrorExtractor from '@keepfy/error-extractor'
import { ApolloError } from 'apollo-client'
const {
type,
message,
title
} = KeepfyErrorExtractor.getSuggestion('UNKNOWN_ERROR')
// or from apollo error response
mutate(...options)
.catch((error: ApolloError) => {
const suggestion = KeepfyErrorExtractor.suggestionFromGraphQLError(error)
// do something with the error .message or .title
})
```