Skip to content

Commit c8a6108

Browse files
authored
[fix] Migration fixes (#6096)
* handle error without message * handle GET without body (which is wrong, but people could have that) * automigrate status 200 in load * changeset
1 parent b9d709a commit c8a6108

File tree

5 files changed

+94
-7
lines changed

5 files changed

+94
-7
lines changed

.changeset/olive-ducks-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte-migrate': patch
3+
---
4+
5+
Handle Error without message, handle status 200, handle missing body

packages/migrate/migrations/routes/migrate_page_js/index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ export function migrate_page(content, filename) {
6161
return; // nothing to do
6262
}
6363

64-
if (keys === 'props') {
65-
automigration(value, file.code, dedent(nodes.props.getText()));
64+
if (
65+
keys === 'props' ||
66+
((keys === 'status' || keys === 'props status') &&
67+
Number(nodes.status.getText()) === 200)
68+
) {
69+
automigration(value, file.code, dedent(nodes.props?.getText() || ''));
6670
return;
6771
}
6872

@@ -85,10 +89,16 @@ export function migrate_page(content, filename) {
8589
if (nodes.error) {
8690
const message = is_string_like(nodes.error)
8791
? nodes.error.getText()
88-
: is_new(nodes.error, 'Error') && nodes.error.arguments[0].getText();
89-
90-
if (message) {
91-
automigration(node, file.code, `throw error(${status || 500}, ${message});`);
92+
: is_new(nodes.error, 'Error')
93+
? /** @type {string | undefined} */ (nodes.error.arguments[0]?.getText())
94+
: false;
95+
96+
if (message !== false) {
97+
automigration(
98+
node,
99+
file.code,
100+
`throw error(${status || 500}${message ? `, ${message}` : ''});`
101+
);
92102
imports.add('error');
93103
return;
94104
}

packages/migrate/migrations/routes/migrate_page_js/samples.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,29 @@ export function load({ session }) {
157157
}
158158
```
159159
160+
## Error constructor with no arguments
161+
162+
```js before
163+
export function load({ session }) {
164+
if (!session.user?.admin) {
165+
return {
166+
status: 403,
167+
error: new Error()
168+
};
169+
}
170+
}
171+
```
172+
173+
```js after
174+
import { error } from '@sveltejs/kit';
175+
176+
export function load({ session }) {
177+
if (!session.user?.admin) {
178+
throw error(403);
179+
}
180+
}
181+
```
182+
160183
## Error status with no error
161184
162185
```js before
@@ -322,3 +345,36 @@ export function load() {
322345
return;
323346
}
324347
```
348+
349+
## A load function that returns props and status 200
350+
351+
```js before
352+
export function load() {
353+
return {
354+
status: 200,
355+
props: {}
356+
};
357+
}
358+
```
359+
360+
```js after
361+
export function load() {
362+
return {};
363+
}
364+
```
365+
366+
## A load function that returns status 200
367+
368+
```js before
369+
export function load() {
370+
return {
371+
status: 200
372+
};
373+
}
374+
```
375+
376+
```js after
377+
export function load() {
378+
return ;
379+
}
380+
```

packages/migrate/migrations/routes/migrate_page_server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function migrate_page_server(content, filename) {
5858
return;
5959
}
6060

61-
automigration(value, file.code, dedent(nodes.body.getText()));
61+
automigration(value, file.code, dedent(nodes.body?.getText() || ''));
6262
});
6363
}
6464

packages/migrate/migrations/routes/migrate_page_server/samples.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,19 @@ export function load() {
137137
return;
138138
}
139139
```
140+
141+
## A function that wrongfully has no body
142+
143+
```js before
144+
export function GET() {
145+
return {
146+
status: 200
147+
};
148+
}
149+
```
150+
151+
```js after
152+
export function load() {
153+
return ;
154+
}
155+
```

0 commit comments

Comments
 (0)