-
Notifications
You must be signed in to change notification settings - Fork 35
/
http.ts
164 lines (151 loc) · 5.82 KB
/
http.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { BasePineconeError } from './base';
/** @internal */
export type FailedRequestInfo = {
status: number;
url?: string;
body?: string;
message?: string;
};
const CONFIG_HELP = `You can find the configuration values for your project in the Pinecone developer console at https://app.pinecone.io`;
/** This error is thrown when API requests return with status 400. Typically this is due to some aspect of the request being incorrect or invalid.
*
* Some examples when this error could occur:
* - While attempting to create an index with no available quota in your project.
* - While upserting records that do not match the `dimension` of your index
* - While attempting to create an index using an invalid name ("!@#$%")
*/
export class PineconeBadRequestError extends BasePineconeError {
constructor(failedRequest: FailedRequestInfo) {
const { message } = failedRequest;
super(message);
this.name = 'PineconeBadRequestError';
}
}
/**
* This error occurs when API requests are attempted using invalid configurations such as a mispelled or revoked API key.
*
* Log in to https://app.pinecone.io to verify you have configured the { @link Pinecone }
* client using the correct values.
*/
export class PineconeAuthorizationError extends BasePineconeError {
constructor(failedRequest: FailedRequestInfo) {
const { url } = failedRequest;
if (url) {
super(
`The API key you provided was rejected while calling ${url}. Please check your configuration values and try again. ${CONFIG_HELP}`
);
} else {
super(
`The API key you provided was rejected. Please check your configuration values and try again. ${CONFIG_HELP}`
);
}
this.name = 'PineconeAuthorizationError';
}
}
/**
* This error is thrown when interacting with a resource such as an index or collection
* that cannot be found.
*/
export class PineconeNotFoundError extends BasePineconeError {
constructor(failedRequest: FailedRequestInfo) {
const { url } = failedRequest;
if (url) {
super(`A call to ${url} returned HTTP status 404.`);
} else {
super('The requested resource could not be found.');
}
this.name = 'PineconeNotFoundError';
}
}
/**
* This error is thrown when attempting to create a resource such as an index or
* collection with a name that is already in use.
* */
export class PineconeConflictError extends BasePineconeError {
constructor(failedRequest: FailedRequestInfo) {
const { url, message } = failedRequest;
if (url) {
super(
`A call to ${url} returned HTTP status 409. ${message ? message : ''}`
);
} else {
super('The resource you are attempting to create already exists.');
}
this.name = 'PineconeConflictError';
}
}
/**
* This error indicates API responses are returning with status 500 and
* something is wrong with Pinecone. Check the [status page](https://status.pinecone.io/)
* for information about current or recent outages.
*
* @see [Pinecone's status page](https://status.pinecone.io/)
*/
export class PineconeInternalServerError extends BasePineconeError {
constructor(failedRequest: FailedRequestInfo) {
const { url, body } = failedRequest;
const intro = url
? `An internal server error occured while calling the ${url} endpoint.`
: '';
const help = `To see overall service health and learn whether this seems like a large-scale problem or one specific to your request, please go to https://status.pinecone.io/ to view our status page. If you believe the error reflects a problem with this client, please file a bug report in the github issue tracker at https://github.com/pinecone-io/pinecone-ts-client`;
const bodyMessage = body ? `Body: ${body}` : '';
super([intro, help, bodyMessage].join(' ').trim());
this.name = 'PineconeInternalServerError';
}
}
/**
* This error is thrown when you are attempting to use a feature that is
* not implemented or unavailable to you on your current plan. Free indexes
* only support a subset of Pinecone's capabilities, and if you are seeing
* these exceptions then you should consult the
* [pricing page](https://www.pinecone.io/pricing/) to see whether upgrading
* makes sense for your use case.
*/
export class PineconeNotImplementedError extends BasePineconeError {
constructor(requestInfo: FailedRequestInfo) {
const { url, message } = requestInfo;
if (url) {
super(
`A call to ${url} returned HTTP status 501. ${message ? message : ''}`
);
} else {
super();
}
this.name = 'PineconeNotImplementedError';
}
}
/**
* This catch-all exception is thrown when a request error that is not
* specifically mapped to another exception is thrown.
*/
export class PineconeUnmappedHttpError extends BasePineconeError {
constructor(failedRequest: FailedRequestInfo) {
const { url, status, body, message } = failedRequest;
const intro = url
? `An unexpected error occured while calling the ${url} endpoint. `
: '';
const statusMsg = status ? `Status: ${status}. ` : '';
const bodyMsg = body ? `Body: ${body}` : '';
super([intro, message, statusMsg, bodyMsg].join(' ').trim());
this.name = 'PineconeUnmappedHttpError';
}
}
/** @internal */
export const mapHttpStatusError = (failedRequestInfo: FailedRequestInfo) => {
switch (failedRequestInfo.status) {
case 400:
return new PineconeBadRequestError(failedRequestInfo);
case 401:
return new PineconeAuthorizationError(failedRequestInfo);
case 404:
return new PineconeNotFoundError(failedRequestInfo);
case 409:
return new PineconeConflictError(failedRequestInfo);
case 500:
return new PineconeInternalServerError(failedRequestInfo);
case 501:
return new PineconeNotImplementedError(failedRequestInfo);
default:
throw new PineconeUnmappedHttpError(failedRequestInfo);
}
};