fix: improve POST payload and error reporting in compose
- Omit image field from content when no image is selected (sending image:[] likely fails the API's schema validation with a 400) - Extract full axios response body in the error alert so the actual API error message is visible instead of just the HTTP status line Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { Feather } from "@expo/vector-icons";
|
import { Feather } from "@expo/vector-icons";
|
||||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
|
import axios from "axios";
|
||||||
import DateTimePicker from "@react-native-community/datetimepicker";
|
import DateTimePicker from "@react-native-community/datetimepicker";
|
||||||
import * as Haptics from "expo-haptics";
|
import * as Haptics from "expo-haptics";
|
||||||
import { Image } from "expo-image";
|
import { Image } from "expo-image";
|
||||||
@@ -129,10 +130,15 @@ export default function ComposeScreen() {
|
|||||||
media = [{ id: uploaded.id, path: uploaded.path }];
|
media = [{ id: uploaded.id, path: uploaded.path }];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const contentItem: { content: string; image?: Array<{ id: string; path: string }> } = {
|
||||||
|
content: content.trim(),
|
||||||
|
};
|
||||||
|
if (media.length > 0) contentItem.image = media;
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
type: postNow ? "now" : "schedule",
|
type: postNow ? "now" : "schedule",
|
||||||
date: postNow ? new Date().toISOString() : scheduleDate.toISOString(),
|
date: postNow ? new Date().toISOString() : scheduleDate.toISOString(),
|
||||||
content: [{ content: content.trim(), image: media }],
|
content: [contentItem],
|
||||||
integrations: selectedChannels,
|
integrations: selectedChannels,
|
||||||
};
|
};
|
||||||
await client.post("posts", payload);
|
await client.post("posts", payload);
|
||||||
@@ -147,7 +153,15 @@ export default function ComposeScreen() {
|
|||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
|
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
|
||||||
let msg = "Could not submit post.";
|
let msg = "Could not submit post.";
|
||||||
if (e instanceof Error) msg += `\n${e.message}`;
|
if (axios.isAxiosError(e)) {
|
||||||
|
const data = e.response?.data;
|
||||||
|
const detail = data
|
||||||
|
? (typeof data === "string" ? data : (data?.message ?? data?.error ?? JSON.stringify(data))).toString().slice(0, 300)
|
||||||
|
: e.message;
|
||||||
|
msg = `HTTP ${e.response?.status ?? "?"}: ${detail}`;
|
||||||
|
} else if (e instanceof Error) {
|
||||||
|
msg += `\n${e.message}`;
|
||||||
|
}
|
||||||
Alert.alert("Failed", msg);
|
Alert.alert("Failed", msg);
|
||||||
} finally {
|
} finally {
|
||||||
setSubmitting(false);
|
setSubmitting(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user