Skip to content

Commit 29c9021

Browse files
authored
Replace throw on missing env with warning (#1311)
* fix: replace throw on missing env with warning * fix: check url/token only if they exist * fix: only replace if baseUrl exists * fix: rm throw from client because of vercel pre-rendering, the error was thrown when turbo was used * fix: rm platform tests
1 parent ef1ca98 commit 29c9021

File tree

5 files changed

+69
-45
lines changed

5 files changed

+69
-45
lines changed

pkg/error.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class UpstashError extends Error {
1111
export class UrlError extends Error {
1212
constructor(url: string) {
1313
super(
14-
`Upstash Redis client was passed an invalid URL. You should pass the URL together with https. Received: "${url}". `
14+
`Upstash Redis client was passed an invalid URL. You should pass a URL starting with https. Received: "${url}". `
1515
);
1616
this.name = "UrlError";
1717
}

pkg/http.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export class HttpClient implements Requester {
127127
};
128128
public readYourWrites: boolean;
129129
public upstashSyncToken = "";
130+
private hasCredentials: boolean;
130131

131132
public readonly retry: {
132133
attempts: number;
@@ -145,7 +146,7 @@ export class HttpClient implements Requester {
145146
this.upstashSyncToken = "";
146147
this.readYourWrites = config.readYourWrites ?? true;
147148

148-
this.baseUrl = config.baseUrl.replace(/\/$/, "");
149+
this.baseUrl = (config.baseUrl || "").replace(/\/$/, "");
149150

150151
/**
151152
* regex to check if the baseUrl starts with http:// or https://
@@ -157,7 +158,7 @@ export class HttpClient implements Requester {
157158
* - `$` asserts the position at the end of the string.
158159
*/
159160
const urlRegex = /^https?:\/\/[^\s#$./?].\S*$/;
160-
if (!urlRegex.test(this.baseUrl)) {
161+
if (this.baseUrl && !urlRegex.test(this.baseUrl)) {
161162
throw new UrlError(this.baseUrl);
162163
}
163164

@@ -167,6 +168,8 @@ export class HttpClient implements Requester {
167168
...config.headers,
168169
};
169170

171+
this.hasCredentials = Boolean(this.baseUrl && this.headers.authorization.split(" ")[1]);
172+
170173
if (this.options.responseEncoding === "base64") {
171174
this.headers["Upstash-Encoding"] = "base64";
172175
}
@@ -206,6 +209,13 @@ export class HttpClient implements Requester {
206209
backend: this.options.backend,
207210
};
208211

212+
if (!this.hasCredentials) {
213+
console.warn(
214+
"[Upstash Redis] Redis client was initialized without url or token." +
215+
" Failed to execute command."
216+
);
217+
}
218+
209219
/**
210220
* We've recieved a new `upstash-sync-token` in the previous response. We use it in the next request to observe the effects of previous requests.
211221
*/

platforms/cloudflare.ts

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
21
import type { RequesterConfig } from "../pkg/http";
32
import { HttpClient } from "../pkg/http";
43
import * as core from "../pkg/redis";
@@ -56,27 +55,32 @@ export class Redis extends core.Redis {
5655
*/
5756
constructor(config: RedisConfigCloudflare, env?: Env) {
5857
if (!config.url) {
59-
throw new Error(
58+
console.warn(
6059
`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`
6160
);
61+
} else if (config.url.startsWith(" ") || config.url.endsWith(" ") || /\r|\n/.test(config.url)) {
62+
console.warn(
63+
"[Upstash Redis] The redis url contains whitespace or newline, which can cause errors!"
64+
);
6265
}
6366

6467
if (!config.token) {
65-
throw new Error(
68+
console.warn(
6669
`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`
6770
);
68-
}
69-
70-
if (config.url.startsWith(" ") || config.url.endsWith(" ") || /\r|\n/.test(config.url)) {
71-
console.warn("The redis url contains whitespace or newline, which can cause errors!");
72-
}
73-
if (config.token.startsWith(" ") || config.token.endsWith(" ") || /\r|\n/.test(config.token)) {
74-
console.warn("The redis token contains whitespace or newline, which can cause errors!");
71+
} else if (
72+
config.token.startsWith(" ") ||
73+
config.token.endsWith(" ") ||
74+
/\r|\n/.test(config.token!)
75+
) {
76+
console.warn(
77+
"[Upstash Redis] The redis token contains whitespace or newline, which can cause errors!"
78+
);
7579
}
7680

7781
const client = new HttpClient({
7882
retry: config.retry,
79-
baseUrl: config.url,
83+
baseUrl: config.url!,
8084
headers: { authorization: `Bearer ${config.token}` },
8185
responseEncoding: config.responseEncoding,
8286
signal: config.signal,
@@ -127,13 +131,13 @@ export class Redis extends core.Redis {
127131
const token = env?.UPSTASH_REDIS_REST_TOKEN ?? UPSTASH_REDIS_REST_TOKEN;
128132

129133
if (!url) {
130-
throw new Error(
131-
"Unable to find environment variable: `UPSTASH_REDIS_REST_URL`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_URL`"
134+
console.warn(
135+
"[Upstash Redis] Unable to find environment variable: `UPSTASH_REDIS_REST_URL`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_URL`"
132136
);
133137
}
134138
if (!token) {
135-
throw new Error(
136-
"Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`"
139+
console.warn(
140+
"[Upstash Redis] Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`"
137141
);
138142
}
139143
return new Redis({ ...opts, url, token }, env);

platforms/fastly.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,31 @@ export class Redis extends core.Redis {
5353
*/
5454
constructor(config: RedisConfigFastly) {
5555
if (!config.url) {
56-
throw new Error(
56+
console.warn(
5757
`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`
5858
);
59+
} else if (config.url.startsWith(" ") || config.url.endsWith(" ") || /\r|\n/.test(config.url)) {
60+
console.warn(
61+
"[Upstash Redis] The redis url contains whitespace or newline, which can cause errors!"
62+
);
5963
}
6064

6165
if (!config.token) {
62-
throw new Error(
66+
console.warn(
6367
`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`
6468
);
65-
}
66-
67-
if (config.url.startsWith(" ") || config.url.endsWith(" ") || /\r|\n/.test(config.url)) {
68-
console.warn("The redis url contains whitespace or newline, which can cause errors!");
69-
}
70-
if (config.token.startsWith(" ") || config.token.endsWith(" ") || /\r|\n/.test(config.token)) {
71-
console.warn("The redis token contains whitespace or newline, which can cause errors!");
69+
} else if (
70+
config.token.startsWith(" ") ||
71+
config.token.endsWith(" ") ||
72+
/\r|\n/.test(config.token)
73+
) {
74+
console.warn(
75+
"[Upstash Redis] The redis token contains whitespace or newline, which can cause errors!"
76+
);
7277
}
7378

7479
const client = new HttpClient({
75-
baseUrl: config.url,
80+
baseUrl: config.url!,
7681
retry: config.retry,
7782
headers: { authorization: `Bearer ${config.token}` },
7883
options: { backend: config.backend },

platforms/nodejs.ts

+22-17
Original file line numberDiff line numberDiff line change
@@ -102,34 +102,35 @@ export class Redis extends core.Redis {
102102
}
103103

104104
if (!configOrRequester.url) {
105-
throw new Error(
105+
console.warn(
106106
`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`
107107
);
108-
}
109-
110-
if (!configOrRequester.token) {
111-
throw new Error(
112-
`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`
113-
);
114-
}
115-
116-
if (
108+
} else if (
117109
configOrRequester.url.startsWith(" ") ||
118110
configOrRequester.url.endsWith(" ") ||
119111
/\r|\n/.test(configOrRequester.url)
120112
) {
121-
console.warn("The redis url contains whitespace or newline, which can cause errors!");
113+
console.warn(
114+
"[Upstash Redis] The redis url contains whitespace or newline, which can cause errors!"
115+
);
122116
}
123-
if (
117+
118+
if (!configOrRequester.token) {
119+
console.warn(
120+
`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`
121+
);
122+
} else if (
124123
configOrRequester.token.startsWith(" ") ||
125124
configOrRequester.token.endsWith(" ") ||
126125
/\r|\n/.test(configOrRequester.token)
127126
) {
128-
console.warn("The redis token contains whitespace or newline, which can cause errors!");
127+
console.warn(
128+
"[Upstash Redis] The redis token contains whitespace or newline, which can cause errors!"
129+
);
129130
}
130131

131132
const client = new HttpClient({
132-
baseUrl: configOrRequester.url,
133+
baseUrl: configOrRequester.url!,
133134
retry: configOrRequester.retry,
134135
headers: { authorization: `Bearer ${configOrRequester.token}` },
135136

@@ -175,18 +176,22 @@ export class Redis extends core.Redis {
175176

176177
if (process.env === undefined) {
177178
throw new TypeError(
178-
'Unable to get environment variables, `process.env` is undefined. If you are deploying to cloudflare, please import from "@upstash/redis/cloudflare" instead'
179+
'[Upstash Redis] Unable to get environment variables, `process.env` is undefined. If you are deploying to cloudflare, please import from "@upstash/redis/cloudflare" instead'
179180
);
180181
}
182+
181183
// @ts-ignore process will be defined in node
182184
const url = process.env.UPSTASH_REDIS_REST_URL || process.env.KV_REST_API_URL;
183185
if (!url) {
184-
throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_URL`");
186+
console.warn("[Upstash Redis] Unable to find environment variable: `UPSTASH_REDIS_REST_URL`");
185187
}
188+
186189
// @ts-ignore process will be defined in node
187190
const token = process.env.UPSTASH_REDIS_REST_TOKEN || process.env.KV_REST_API_TOKEN;
188191
if (!token) {
189-
throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
192+
console.warn(
193+
"[Upstash Redis] Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`"
194+
);
190195
}
191196
return new Redis({ ...config, url, token });
192197
}

0 commit comments

Comments
 (0)