✨ Handle user not found
This commit is contained in:
1 parent
ce2cdfd409
commit
95d7d13f38
5 files changed
+53
-3
No files matched your search
+2
-1
@@ -11,6 +11,7 @@ CACHE/
|
|||||||
media/
|
media/
|
||||||
tmp/
|
tmp/
|
||||||
frontend/dist
|
frontend/dist
|
||||||
static/
|
static/assets
|
||||||
|
static/index.html
|
||||||
.python-version
|
.python-version
|
||||||
.env
|
.env
|
||||||
+15
-1
@@ -1,4 +1,6 @@
|
|||||||
import strawberry
|
import strawberry
|
||||||
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
from strawberry.extensions import Extension
|
||||||
|
|
||||||
from bbs.api.mutations.shop import ShopMutations
|
from bbs.api.mutations.shop import ShopMutations
|
||||||
from bbs.api.mutations.users import UsersMutations
|
from bbs.api.mutations.users import UsersMutations
|
||||||
@@ -17,4 +19,16 @@ class Mutation(ShopMutations, UsersMutations):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
schema = strawberry.Schema(query=Query, mutation=Mutation)
|
class HandleNotFound(Extension):
|
||||||
|
def on_request_end(self):
|
||||||
|
result = self.execution_context.result
|
||||||
|
if result.errors:
|
||||||
|
for error in result.errors:
|
||||||
|
if isinstance(error.original_error, ObjectDoesNotExist):
|
||||||
|
error.extensions = {
|
||||||
|
"code": "NOT_FOUND",
|
||||||
|
"model": type(error.original_error).__qualname__.replace(".DoesNotExist", ""),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
schema = strawberry.Schema(query=Query, mutation=Mutation, extensions=[HandleNotFound])
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
.errorMessage {
|
||||||
|
font-family: Rocket Rinder, monospace;
|
||||||
|
font-size: 3rem;
|
||||||
|
font-style: italic;
|
||||||
|
color: var(--brand);
|
||||||
|
text-align: center;
|
||||||
|
width: 100%;
|
||||||
|
margin-top: 30vh;
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { ApolloError } from "@apollo/client";
|
||||||
|
import React from "react";
|
||||||
|
|
||||||
|
import styles from "./GQLError.module.css";
|
||||||
|
|
||||||
|
const formatModel = (text: string) => {
|
||||||
|
const withSpaces = text
|
||||||
|
.replace(/([A-Z])/g, " $1")
|
||||||
|
.toLocaleLowerCase()
|
||||||
|
.trim();
|
||||||
|
return withSpaces.charAt(0).toUpperCase() + withSpaces.slice(1);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const GQLError = ({ error }: { error: ApolloError }) => {
|
||||||
|
if (error?.graphQLErrors?.length == 1) {
|
||||||
|
const single = error?.graphQLErrors[0];
|
||||||
|
if (single?.extensions?.code === "NOT_FOUND") {
|
||||||
|
const modelPretty = formatModel(single?.extensions?.model as string);
|
||||||
|
return <div className={styles.errorMessage}>{`${modelPretty} not found`}</div>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return <div className={styles.errorMessage}>Error...</div>;
|
||||||
|
};
|
||||||
@@ -3,16 +3,19 @@ import { Line } from "@nivo/line";
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
|
|
||||||
|
import { GQLError } from "../../components/GQLError";
|
||||||
import { User, UserDetailDocument, UserProfile } from "../../types";
|
import { User, UserDetailDocument, UserProfile } from "../../types";
|
||||||
import { formatMonth, formatTime } from "../../utils/time";
|
import { formatMonth, formatTime } from "../../utils/time";
|
||||||
import styles from "./UserDetail.module.css";
|
import styles from "./UserDetail.module.css";
|
||||||
|
|
||||||
const UserDetail = () => {
|
const UserDetail = () => {
|
||||||
const { userId } = useParams();
|
const { userId } = useParams();
|
||||||
const { data, loading } = useQuery(UserDetailDocument, { variables: { userId: userId || "" }, skip: !userId });
|
const { data, loading, error } = useQuery(UserDetailDocument, { variables: { userId: userId || "" }, skip: !userId });
|
||||||
|
|
||||||
if (loading) return <div>Loading...</div>;
|
if (loading) return <div>Loading...</div>;
|
||||||
|
|
||||||
|
if (error) return <GQLError error={error} />;
|
||||||
|
|
||||||
const user = data?.user as User;
|
const user = data?.user as User;
|
||||||
const profile = user?.profile as UserProfile;
|
const profile = user?.profile as UserProfile;
|
||||||
// FIXME: types
|
// FIXME: types
|
||||||
|
|||||||
Reference in new issue
Block a user