Skip to content

Commit d22fb98

Browse files
authored
Merge pull request #8 from sqliteai/fix-error-messages
Handle error messages from response body in every network backend like curl
2 parents 880c862 + ba20582 commit d22fb98

File tree

11 files changed

+79
-26
lines changed

11 files changed

+79
-26
lines changed

Makefile

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ endif
136136
T_LDFLAGS += -fprofile-arcs -ftest-coverage
137137
endif
138138

139+
# Native network support only for Apple platforms
140+
ifdef NATIVE_NETWORK
141+
RELEASE_OBJ += $(patsubst %.m, $(BUILD_RELEASE)/%_m.o, $(notdir $(wildcard $(SRC_DIR)/*.m)))
142+
LDFLAGS += -framework Foundation
143+
CFLAGS += -DCLOUDSYNC_OMIT_CURL
144+
145+
$(BUILD_RELEASE)/%_m.o: %.m
146+
$(CC) $(CFLAGS) -fobjc-arc -O3 -fPIC -c $< -o $@
147+
endif
148+
139149
# Windows .def file generation
140150
$(DEF_FILE):
141151
ifeq ($(PLATFORM),windows)
@@ -314,15 +324,15 @@ clean:
314324
help:
315325
@echo "SQLite Sync Extension Makefile"
316326
@echo "Usage:"
317-
@echo " make [PLATFORM=platform] [ARCH=arch] [ANDROID_NDK=\$$ANDROID_HOME/ndk/26.1.10909125] [target]"
327+
@echo " make [PLATFORM=platform] [ARCH=arch] [ANDROID_NDK=\$$ANDROID_HOME/ndk/26.1.10909125] [NATIVE_NETWORK=ON] [target]"
318328
@echo ""
319329
@echo "Platforms:"
320330
@echo " linux (default on Linux)"
321-
@echo " macos (default on macOS)"
331+
@echo " macos (default on macOS - can be compiled with native network support)"
322332
@echo " windows (default on Windows)"
323333
@echo " android (needs ARCH to be set to x86_64 or arm64-v8a and ANDROID_NDK to be set)"
324-
@echo " ios (only on macOS)"
325-
@echo " isim (only on macOS)"
334+
@echo " ios (only on macOS - can be compiled with native network support)"
335+
@echo " isim (only on macOS - can be compiled with native network support)"
326336
@echo " wasm (needs wabt[brew install wabt/sudo apt install wabt])"
327337
@echo ""
328338
@echo "Targets:"

examples/sport-tracker-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"vite": "^7.0.0"
1414
},
1515
"dependencies": {
16-
"@sqliteai/sqlite-sync-wasm": "^3.49.2-sync-0.8.9",
16+
"@sqliteai/sqlite-sync-wasm": "^3.49.2-sync-0.8.20",
1717
"@types/react": "^19.1.8",
1818
"@types/react-dom": "^19.1.6",
1919
"@vitejs/plugin-react": "^4.6.0",

examples/sport-tracker-app/src/App.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const AppContent: React.FC = () => {
3636
const [refreshTrigger, setRefreshTrigger] = useState(0);
3737
const [sqliteSyncVersion, setSqliteSyncVersion] = useState<string>("");
3838
const [sqliteVersion, setSqliteVersion] = useState<string>("");
39+
const [loginError, setLoginError] = useState<string>("");
3940

4041
// Coach mode - true when logged in user is named "coach"
4142
const isCoachMode = (session: UserSession | null): boolean => {
@@ -392,6 +393,15 @@ const AppContent: React.FC = () => {
392393
);
393394
}
394395

396+
if (loginError) {
397+
return (
398+
<div className="error-container">
399+
<h2>Login Error</h2>
400+
<p>{loginError}</p>
401+
</div>
402+
);
403+
}
404+
395405
if (!isInitialized || loading) {
396406
return (
397407
<div className="loading-container">
@@ -425,6 +435,7 @@ const AppContent: React.FC = () => {
425435
onLogout={handleLogout}
426436
onUsersLoad={loadUsers}
427437
onRefresh={handleRefreshData}
438+
onError={setLoginError}
428439
/>
429440
<DatabaseStatus
430441
onCountsLoad={loadCounts}

examples/sport-tracker-app/src/SQLiteSync.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ export class SQLiteSync {
6060
throw new Error("Database not available");
6161
}
6262

63-
try {
64-
await this.db.sqliteSyncNetworkSync();
65-
} catch (e) {
66-
console.error("Error checking SQLite Sync changes:", e);
67-
}
63+
await this.db.sqliteSyncNetworkSync();
6864
}
6965

7066
/**

examples/sport-tracker-app/src/components/UserLogin.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface UserLoginProps {
1515
onLogout: () => Promise<void>;
1616
onUsersLoad: () => void;
1717
onRefresh: () => void;
18+
onError?: (error: string) => void; // Optional error handler
1819
}
1920

2021
const UserLogin: React.FC<UserLoginProps> = ({
@@ -24,6 +25,7 @@ const UserLogin: React.FC<UserLoginProps> = ({
2425
onLogout,
2526
onUsersLoad,
2627
onRefresh,
28+
onError
2729
}) => {
2830
const { db } = useDatabase();
2931
const [selectedUserId, setSelectedUserId] = useState<string>("");
@@ -113,6 +115,7 @@ const UserLogin: React.FC<UserLoginProps> = ({
113115
error
114116
);
115117
console.warn("SQLite Sync: Falling back to local refresh only");
118+
if(onError) onError("SQLite Sync - Failed to sync with SQLite Cloud: " + error);
116119
}
117120
} else {
118121
console.log(

examples/sport-tracker-app/src/db/database.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ export class Database {
9797
return this.sendMessage("sqliteSyncSetToken", token);
9898
}
9999

100-
101100
async sqliteSyncNetworkSync(): Promise<void> {
102101
return this.sendMessage("sqliteSyncNetworkSync");
103102
}

src/cloudsync.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "sqlite3.h"
1717
#endif
1818

19-
#define CLOUDSYNC_VERSION "0.8.12"
19+
#define CLOUDSYNC_VERSION "0.8.20"
2020

2121
int sqlite3_cloudsync_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi);
2222

src/dbutils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// dbutils.c
3-
// cloudsync_test
3+
// cloudsync
44
//
55
// Created by Marco Bambini on 23/09/24.
66
//

src/network.m

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// network.m
3-
// cloudsync_network_test
3+
// cloudsync
44
//
55
// Created by Marco Bambini on 23/05/25.
66
//
@@ -58,7 +58,7 @@ bool network_compute_endpoints (sqlite3_context *context, network_data *data, co
5858
}
5959

6060
char *site_id = network_data_get_siteid(data);
61-
char *port_or_default = (port) ? (char *)port.UTF8String : CLOUDSYNC_DEFAULT_ENDPOINT_PORT;
61+
char *port_or_default = (port && strcmp(port.UTF8String, "8860") != 0) ? (char *)port.UTF8String : CLOUDSYNC_DEFAULT_ENDPOINT_PORT;
6262

6363
NSString *check_endpoint = [NSString stringWithFormat:@"%s://%s:%s/%s%s/%s", scheme.UTF8String, host.UTF8String, port_or_default, CLOUDSYNC_ENDPOINT_PREFIX, database.UTF8String, site_id];
6464
NSString *upload_endpoint = [NSString stringWithFormat: @"%s://%s:%s/%s%s/%s/%s", scheme.UTF8String, host.UTF8String, port_or_default, CLOUDSYNC_ENDPOINT_PREFIX, database.UTF8String, site_id, CLOUDSYNC_ENDPOINT_UPLOAD];
@@ -147,15 +147,19 @@ NETWORK_RESULT network_receive_buffer(network_data *data, const char *endpoint,
147147
}
148148

149149
__block NSData *responseData = nil;
150-
__block NSError *responseError = nil;
150+
__block NSString *responseError = nil;
151151
__block NSInteger statusCode = 0;
152+
__block NSInteger errorCode = 0;
152153

153154
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
154155

155156
NSURLSession *session = [NSURLSession sharedSession];
156157
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
157158
responseData = data;
158-
responseError = error;
159+
if (error) {
160+
responseError = [error localizedDescription];
161+
errorCode = [error code];
162+
}
159163
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
160164
statusCode = [(NSHTTPURLResponse *)response statusCode];
161165
}
@@ -166,9 +170,10 @@ NETWORK_RESULT network_receive_buffer(network_data *data, const char *endpoint,
166170
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
167171

168172
if (!responseError && (statusCode >= 200 && statusCode < 300)) {
169-
size_t len = [responseData length];
170173
// check if OK should be returned
171-
if (len == 0) return (NETWORK_RESULT){CLOUDSYNC_NETWORK_OK, NULL, 0, NULL, NULL};
174+
if (responseData == nil || [responseData length] == 0) {
175+
return (NETWORK_RESULT){CLOUDSYNC_NETWORK_OK, NULL, 0, NULL, NULL};
176+
}
172177

173178
// otherwise return a buffer
174179
NETWORK_RESULT result = {};
@@ -181,18 +186,31 @@ NETWORK_RESULT network_receive_buffer(network_data *data, const char *endpoint,
181186
result.buffer = (char *)responseData.bytes;
182187
result.xdata = (void *)CFBridgingRetain(responseData);
183188
}
184-
result.blen = len;
189+
result.blen = [responseData length];
185190
result.xfree = network_buffer_cleanup;
191+
186192
return result;
187193
}
188194

189195
// return error
190196
NETWORK_RESULT result = {};
191-
NSString *msg = (responseError) ? [responseError localizedDescription] : [NSString stringWithCString:"Unknown network URL" encoding:NSUTF8StringEncoding];
197+
NSString *msg = nil;
198+
if (responseError) {
199+
msg = responseError;
200+
} else if (responseData && [responseData length] > 0) {
201+
// Use the actual response body as the error message
202+
msg = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
203+
if (!msg) {
204+
msg = [NSString stringWithFormat:@"HTTP %ld error", (long)statusCode];
205+
}
206+
} else {
207+
msg = [NSString stringWithFormat:@"HTTP %ld error", (long)statusCode];
208+
}
192209
result.code = CLOUDSYNC_NETWORK_ERROR;
193210
result.buffer = (char *)msg.UTF8String;
194211
result.xdata = (void *)CFBridgingRetain(msg);
195212
result.xfree = network_buffer_cleanup;
196-
result.blen = (responseError) ? (size_t)responseError.code : (size_t)statusCode;
213+
result.blen = responseError ? (size_t)errorCode : (size_t)statusCode;
214+
197215
return result;
198216
}

src/wasm.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
//
2+
// wasm.c
3+
// cloudsync
4+
//
5+
// Created by Gioele Cantoni on 25/06/25.
6+
//
7+
18
#ifdef SQLITE_WASM_EXTRA_INIT
29
#define CLOUDSYNC_OMIT_CURL
310

@@ -96,7 +103,6 @@ NETWORK_RESULT network_receive_buffer (network_data *data, const char *endpoint,
96103
}
97104

98105
if (fetch->status >= 200 && fetch->status < 300) {
99-
100106
if (blen > 0 && buffer) {
101107
char *buf = (char*)malloc(blen + 1);
102108
if (buf) {
@@ -112,9 +118,18 @@ NETWORK_RESULT network_receive_buffer (network_data *data, const char *endpoint,
112118
result.code = CLOUDSYNC_NETWORK_ERROR;
113119
if (fetch->statusText && fetch->statusText[0]) {
114120
result.buffer = strdup(fetch->statusText);
121+
result.blen = sizeof(fetch->statusText);
122+
result.xfree = free;
123+
} else if (blen > 0 && buffer) {
124+
char *buf = (char*)malloc(blen + 1);
125+
if (buf) {
126+
memcpy(buf, buffer, blen);
127+
buf[blen] = 0;
128+
result.buffer = buf;
129+
result.blen = blen;
130+
result.xfree = free;
131+
}
115132
}
116-
result.blen = sizeof(fetch->statusText);
117-
result.xfree = free;
118133
}
119134

120135
// cleanup

0 commit comments

Comments
 (0)